How to loop Object.values array?

How to get single values from object.values array?

I am getting this obj in console.log() // {"item": Array(3)}

 {"item": (3) ['21', '14', '12']}

and I want to get single number from object.values // 21 ,14, 12

I am trying this

let j = Object.values(data)
  console.log(j)  // [Array(3)]

>Solution :

Since I am not sure what data is present in the variable data. Let’s assume j is an array of elements.

let j = {"items" : ['21', '14', '12']};

If you want to print each element in the array individually, you can use forEach

j.items.forEach(function(element) {
  console.log(element);
});

This will print

21
14
12

or if you need to print all the elements in a single line without a loop, You can use join

let result = j.items.join(",");
console.log(result);

which will print

21,14,12

You can replace the , with whatever separator you wish to use.

let j = {"items" : ['21', '14', '12']};

console.log("List with loop");
j.items.forEach(function(element) {
  console.log(element);
});

console.log("List with join");
let result = j.items.join(",");
console.log(result);

Leave a Reply