Reputation: 77
I need that kind of thing. but I don't want to write that 6 lines manually but instead of that I want to write some loop thing so I can easily get that all six value from one line
console.log(array[0].name.LINE1)
console.log(array[0].name.LINE2)
console.log(array[0].name.LINE3)
console.log(array[1].name.LINE1)
console.log(array[1].name.LINE2)
console.log(array[1].name.LINE3)
From the array
const array = [{name: {LINE1: "1.5", LINE2: "1.1", LINE3: "1.6"}},
{name: {LINE1: "2.5", LINE2: "1.8", LINE3: "1.4"}}]
So I try to do this thing like
for(i=0;i<array.length;i++){
console.log(array[i].name.LINE{i+1})
}
but it doesn't work, can anyone give me a solution how can I destruct that array with the dynamic value? How I can take the value from the array to writing one line instead of that 6 lines??
Upvotes: 0
Views: 356
Reputation: 2829
If you want to log all of the lines you can use .forEach
to loop over the objects inside the array
and then loop over the lines properties inside the name
object
const array = [
{
name: {LINE1: "1.5", LINE2: "1.1", LINE3: "1.6"}
}, {
name: {LINE1: "2.5", LINE2: "1.8", LINE3: "1.4"}
}
];
array.forEach(obj => {
for(let line in obj.name) {
console.log(`${line}: ${obj.name[line]}`);
}
});
Upvotes: 1
Reputation: 11001
Try with forEach
and Object.values
const array = [{name: {LINE1: "1.5", LINE2: "1.1", LINE3: "1.6"}},
{name: {LINE1: "2.5", LINE2: "1.8", LINE3: "1.4"}}]
array.forEach(({name}) => console.log(Object.values(name)))
array.forEach(({name}) => console.log(Object.entries(name)))
Upvotes: 0