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 get a value from an Array of object without calling the key (i've searched through the internet can't found the exact/similar problem to this)

So i had this kind of const value

    const testingObjArr = [
    {
      asdasdasdasdds:"testing 123"
    },
    {
      bcdefghasdkl:"testing 456"
    },
    {
      asdklqwoepskalx:"testing 678"
    },
 ]

I want to getting the value only and push that into a new array
like this

["testing 123", "testing 456", "testing 678"]

I’ve tried using Object.values and similar ways but always return the same values when log it.

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

this is the last method that i used

console.log( "testing objs",Object.values(obj).map(i => i).flat())

I also use this loop to getting the values

const testingObjArrLength = Object.keys(testingObjArr).length;
const finalDatas = [];
for(let x=0; x<testingObjArrLength; x++) {
  const datas = Object.values(testingObjArr)[x]
  finalDatas.push(datas);
}
console.log("finaldatas", finalDatas);

This is the results in console log

enter image description here

can anyone give me an advice? appreciate that if not tagged this with duplicate, because i’ve been around searching for the solutions

>Solution :

You could take Array#flatMap with Object.values as callback.

const
    data = [{ asdasdasdasdds: "testing 123" }, { bcdefghasdkl: "testing 456" }, { asdklqwoepskalx: "testing 678" }],
    values = data.flatMap(Object.values);

console.log(values);
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