1

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)

3
  • Use the map method: data.map((obj, idx) => obj["path" + (idx + 1)]) Commented Nov 16, 2021 at 6:16
  • @Yousaf take a look at the update code Commented Nov 16, 2021 at 6:26
  • 1
    In the original code, keys of the objects were like: path1, path2, etc. In the updated data array, key is just path. All you need to do is change obj["path" + (idx + 1)] to obj["path"] Commented Nov 16, 2021 at 6:29

3 Answers 3

1

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);

Sign up to request clarification or add additional context in comments.

Comments

1

Use for loop for iterate data

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

const dataValues = [];

for(var i = 0; i < data.length; i++)
{
    dataValues.push(Object.values(data[i]).toString())
}

console.log(dataValues)

Comments

1

This may help You. Try it out..

function nestedLoop(obj) {
    const res = {};
    function recurse(obj, current) {
        for (const key in obj) {
            let value = obj[key];
            if(value != undefined) {
                if (value && typeof value === 'object') {
                    recurse(value, key);
                } else {
                    // Do your stuff here to var value
                    res[key] = value;
                }
            }
        }
    }
    recurse(obj);
    return res;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.