JavaScript:如何在迭代发生时修改数组中的值?

时间:2016-05-04 04:22:17

标签: javascript arrays node.js

嗨Javascript新手问题,

var aarr = [
 {
    "label": "Name",
    "visible": true,
    "value": "HOW"
 },
 {
    "label": "Age",
    "visible": false,
    "value": "23"
 },
 {
    "label": "goodToGo",
    "visible": false,
    "value": "Yes"
 },
 {
    "label": "authenticatedflag",
    "visible": true,
    "value": "No"
 }
];

场景:(如何在单循环中实现此逻辑)

需要根据以下条件修改上述数组:

  

例如:if label ===“Name”&& “value”===“HOW”然后将Age.value修改为   “27”和authenticatedflag.value为“No”,在同一个循环中,   label ===“Age”&& “value”===“27”然后,修改goodToGo.value   否和authenticatedflag.value为“是”

部分代码会。

aarr.map(aObj => {
    if(aObj.label === "Name" && aObj.value === "HOW") {
        // In this case i need to modify the aarr contents
    }
    // Before this loop executes the aarr contents should be changed.
    if(aObj.label === "Age" && aObj.value === "27") {
        // Once again modify the contents of the array before passing to next condition
    }
});

评论No:2 @Tushar似乎工作了一半

if (arr.find(o => o.label === 'Name' && o.value === 'HOW')) {
    arr.find(o => o.label === 'Age').value = 27;
    arr.find(o => o.label === 'authenticatedflag').value = 'No';
} else if (arr.find(o => o.label === 'Age' && o.value === '27')) {
    arr.find(o => o.label === 'goodToGo').value = 'No';
    arr.find(o => o.label === 'authenticatedflag').value = 'Yes';
}

console.log(arr);

/*
[ { label: 'Name', visible: true, value: 'HOW' },
  { label: 'Age', visible: false, value: 27 },
  { label: 'goodToGo', visible: false, value: 'Yes' },
  { label: 'authenticatedflag', visible: true, value: 'No' } ]
*/

在这种情况下,只有一个条件得到满足,实际上它需要满足这两个条件。

2 个答案:

答案 0 :(得分:1)

我会将你的数组结构(如果可能的话)修改为更合适的东西:

var obj = {
    "Name": {
        "visible": true,
        "value": "HOW"
    },
    "Age": {
        "visible": false,
        "value": "23"

    },
    "goodToGo": {
        "visible": false,
        "value": "Yes"

    },
    "authenticatedflag": {
        "visible": true,
        "value": "No"

    }
}

这可以帮助您完成以下操作:

if (obj.Name.value === 'HOW') {
    obj.Age.value = 27;
    obj.authenticatedFlag.value = 'No';
}

if (obj.Age.value === 27) {
    obj.goodToGo.value = 'No';
    obj.authenticatedFlag.value = 'Yes';
}

甚至更多。

如果您无法修改结构本身,我仍会尝试转换数组本身:https://jsfiddle.net/43w4c06n/

var arr = [
    {
        "label": "Name",
        "visible": true,
        "value": "HOW"
    }, {
        "label": "Age",
        "visible": false,
        "value": "23"
    }, {
        "label": "goodToGo",
        "visible": false,
        "value": "Yes"
    }, {
        "label": "authenticatedflag",
        "visible": true,
        "value": "No"
    },
];

var res = arr.map(function (item) {
    var result = {};
    result[item.label] = {
        visible: item.visible,
        value: item.value
    };
    return result;
}).reduce(function (acc, it) {
    return Object.assign(acc, it);
});

这会将您的数组转换为我建议的结构(如果您无法更改服务器的响应)。

LE:我现在看到你正在使用ES2015(我很高兴你这样做:))。这是一个更新的小提琴:https://jsfiddle.net/oLkdcrs1/1/

答案 1 :(得分:0)

var arr = [{
    "label": "Name",
    "visible": true,
    "value": "HOW"
}, {
    "label": "Age",
    "visible": false,
    "value": "23"
}, {
    "label": "goodToGo",
    "visible": false,
    "value": "Yes"
}, {
    "label": "authenticatedflag",
    "visible": true,
    "value": "No"
}];

/* if (arr.find(o => o.label === 'Name' && o.value === 'HOW')) {
    arr.find(o => o.label === 'Age').value = 27;
    arr.find(o => o.label === 'authenticatedflag').value = 'No';
} 

if (arr.find(o => o.label === 'Age' && o.value === '27')) {
    arr.find(o => o.label === 'goodToGo').value = 'No';
    arr.find(o => o.label === 'authenticatedflag').value = 'Yes';
}

console.log(arr); */

/*
[ { label: 'Name', visible: true, value: 'HOW' },
  { label: 'Age', visible: false, value: 27 },
  { label: 'goodToGo', visible: false, value: 'Yes' },
  { label: 'authenticatedflag', visible: true, value: 'No' } ]
*/

var a = arr.map(aObj => {
    if(aObj.label === "Name" && aObj.value === "HOW") {
        arr.find(o => o.label === "Age").value = '27';
        arr.find(o => o.label === "authenticatedflag").value = 'No';
    }
    if(aObj.label === "Age" && aObj.value === '27') {
        arr.find(o => o.label === 'goodToGo').value = 'No';
        arr.find(o => o.label === 'authenticatedflag').value = 'Yes';
    }
    return aObj;
});

/*

[ { label: 'Name', visible: true, value: 'HOW' },
  { label: 'Age', visible: false, value: '27' },
  { label: 'goodToGo', visible: false, value: 'No' },
  { label: 'authenticatedflag', visible: true, value: 'Yes' } ]

*/

这似乎满足了我的条件。但这是唯一的方法吗?或者有没有什么好方法可以修改值?