I have a problem with transferring values from a list to an objectarray. The problem looks as following
ObjectArray = [
{Category:"Toys", CounterParty:"Lego", NrOfOccurences:"ToBeFixed", TotalAmount: 232,52},
{Category:"Games", CounterParty:"Minecraft", NrOfOccurences:"ToBeFixed", TotalAmount:190,68},
{Category:"Sports", CounterParty:"Soccer", NrOfOccurences:"ToBeFixed", TotalAmount: 28,95}]
List = [8,1,35]
The numbers in the list correspond with numbers in the objectArray (so 8 needs to be in the toys list, 1 in Games and 35 in Sports).
This is the eventual outcome i would like:
ObjectArray = [
{Category:"Toys", CounterParty:"Lego", NrOfOccurences:"8, TotalAmount: 232,52},
{Category:"Games", CounterParty:"Minecraft", NrOfOccurences:1, TotalAmount:190,68},
{Category:"Sports", CounterParty:"Soccer", NrOfOccurences:35, TotalAmount: 28,95}
Note: This is only a part of a much bigger list so hardcoding won't get the job done.
Hopefully this is enough info to inform anyone about the problem.
Thanks in advance
CodePudding user response:
It's simple using for loop you can do it
let ObjectArray = [
{ Category: "Toys", CounterParty: "Lego", NrOfOccurences: "ToBeFixed", TotalAmount: '232, 52' },
{ Category: "Games", CounterParty: "Minecraft", NrOfOccurences: "ToBeFixed", TotalAmount: '190, 68' },
{ Category: "Sports", CounterParty: "Soccer", NrOfOccurences: "ToBeFixed", TotalAmount: '28, 95' }
]
let List = [8, 1, 35];
for (let i = 0; i < List.length; i ) {
ObjectArray[i].NrOfOccurences = List[i];
}
console.log(ObjectArray, 'ObjectArray');
