javascript how to change property in nested arrays

How to change a property in an array nested in another data array.

How to replace the value of a nested property in such an array of data:

[{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0
:
{commission: -0.05, confirmed: true, currency: ‘usd’, date: ’21:20 | December 05, 2022′, id: 3455, …}
one
:
{commission: -0.04, confirmed: true, currency: ‘usd’, date: ’20:51 | December 05, 2022′, id: 3453, …}
2
:
{commission: -0.05, confirmed: true, currency: ‘usd’, date: ’20:25 | December 05, 2022′, id: 3450, …}

First, let’s see how we can do it in the usual way:

function toggleAll() {
Object.values(items).forEach(function (item, index, array) {
Object.entries(item).forEach(function ([key, value]) {
if (key === ‘confirmed’ && value === false) {
array[index][key] = true;
} else if (key === ‘confirmed’ && value === true) {
array[index][key] = false
}
});
});
}

Another option to do it like this:

function itemsToggle(b) {
items = items.map(obj => ({
obj,
confirmed: true
}));
}

How to create a new data array by such a property:

function createNewArrayForUpdate() {
confirmedArray = [];
let data = []
items.forEach(function (item) {
if (!item.confirmed) {
data = {
‘id’: item.id,
‘confirmed’: false,
}
} else {
data = {
‘id’: item.id,
‘confirmed’: true,
}
}
confirmedArray.push(data)
});
}

Similar Posts

Leave a Reply