使用find更改数组内的特定值。反应本机

时间:2020-09-14 02:40:28

标签: reactjs react-native

let array = [{7: 10}, {10: 9}, {8: 5}, {3: 4}, {6: 7}]

我想更改此对象数组内的特定值, 我知道该怎么做:

array.find(
(item, index) => Object.keys(item) === 7 
? 
console.log('I would like to delete this object or change its value = ' + Object.values(item))
: 
null
);

谢谢

2 个答案:

答案 0 :(得分:1)

这里是例子。您可以通过参数而不是'7'使其成为函数。

let array = [{7: 10}, {10: 9}, {8: 5}, {3: 4}, {6: 7}];

array.forEach(
    (item) => {
        if (Object.keys(item).find(key => key === '7'))
            item['7'] = 8
    });

console.log(array);

答案 1 :(得分:0)

您可以尝试

// Find the index you want to change
const index = array.findIndex(element => {
    return Object.keys(element) == 7
})

//assign it with new value
array[index] = {5:6}