Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading