Loop through nested array to return values of object

I have a nested array and what I was trying to do was get all the values of the object embedded inside the array. I am currently getting each embedded object and calling Object.values to get the values but this method isn’t efficient when the array size is big. Is there a way to loop through the array and return the values of each object? Any help is appreciated. Thanks in advance.

const data = [{"path":"uploads\\20211115000755-package.json"},{"path":"uploads\\20211115012255-index.html"},{"path":"uploads\\20211115014342-dataServerMid.js"},{"path":"uploads\\20211115031212-index.js"},{"path":"uploads\\20211115031218-uploadDataServer.js"},{"path":"uploads\\20211115031232-index.js"},{"path":"uploads\\20211115031244-dataServerMid.js"},{"path":"uploads\\20211115031250-uploadData.css"},{"path":"uploads\\20211115031303-20211115012255-index.html"},{"path":"uploads\\20211115031318-20211115031303-20211115012255-index.html"},{"path":"uploads\\20211115050204-exportsCapture.JPG"},{"path":"uploads\\20211115052347-[FREE] Stunna 4 Vegas x DaBaby x NLE Choppa Type Beat Call of Duty (320 kbps).mp3"},{"path":"uploads\\20211115200304-Readme.docx"},{"path":"uploads\\20211115202751-Visual Artist Series Fall 2019Corrected.docx"},{"path":"uploads\\20211115203354-ln command examples.docx"},{"path":"uploads\\20211115210027-Q2.docx"},{"path":"uploads\\20211116011817-Fall 2019 ABCD Plattsburgh Syllabi Course Description.docx"}]

//change this to loop and return all the values instead of having to call by index
const dataValues = [Object.values(data[0]).toString(), Object.values(data[1]).toString(), Object.values(data[2]).toString()]

console.log(dataValues)

UPDATE: I tried @yousaf’s method. It worked for my 3 item array but not for this:

const data = [{
  "path": "uploads\\20211115000755-package.json"
}, {
  "path": "uploads\\20211115012255-index.html"
}, {
  "path": "uploads\\20211115014342-dataServerMid.js"
}, {
  "path": "uploads\\20211115031212-index.js"
}, {
  "path": "uploads\\20211115031218-uploadDataServer.js"
}, {
  "path": "uploads\\20211115031232-index.js"
}, {
  "path": "uploads\\20211115031244-dataServerMid.js"
}, {
  "path": "uploads\\20211115031250-uploadData.css"
}, {
  "path": "uploads\\20211115031303-20211115012255-index.html"
}, {
  "path": "uploads\\20211115031318-20211115031303-20211115012255-index.html"
}, {
  "path": "uploads\\20211115050204-exportsCapture.JPG"
}, {
  "path": "uploads\\20211115052347-[FREE] Stunna 4 Vegas x DaBaby x NLE Choppa Type Beat Call of Duty (320 kbps).mp3"
}, {
  "path": "uploads\\20211115200304-Readme.docx"
}, {
  "path": "uploads\\20211115202751-Visual Artist Series Fall 2019Corrected.docx"
}, {
  "path": "uploads\\20211115203354-ln command examples.docx"
}, {
  "path": "uploads\\20211115210027-Q2.docx"
}, {
  "path": "uploads\\20211116011817-Fall 2019.docx"
}]

//change this to loop and return all the values instead of having to call by index
const dataValues = data.map((obj, idx) => obj["path" + (idx + 1)])

console.log(dataValues)

>Solution :

Use a for…of to iterate over the array, and then push the value to a new array.

const data=[{path1:"uploads\\20211115000755-package.json"},{path2:"uploads\\20211115012255-index.html"},{path3:"uploads\\20211115014342-dataServerMid.js"}];

const arr = [];

for (const obj of data) {
  const [value] = Object.values(obj);
  arr.push(value);
}

console.log(arr);

Or you can use map.

const data=[{path1:"uploads\\20211115000755-package.json"},{path2:"uploads\\20211115012255-index.html"},{path3:"uploads\\20211115014342-dataServerMid.js"}];

const arr = data.map(obj => {
  const [value] = Object.values(obj);
  return value;
});

console.log(arr);

Leave a Reply