Daniel Smith
Daniel Smith

Reputation: 1734

Javascript - Convert object to array object

Try to convert the following object (which was getting from an API response) to an array object.

const oldObj = {
    Georgia : {
        notes: "lorem ipsum",
        lat: "32.1656",
        long: "82.9001"
    },
    Alabama : {
        notes: "lorem ipsum",
        lat: "32.3182",
        long: "86.9023"
    }
}

My expected like bellow:

const desireArray = [
    {
        name: "Georgia",
        notes: "lorem ipsum",
        lat: "32.1656",
        long: "82.9001"
    },
    {
        name: "Alabama",
        notes: "lorem ipsum",
        lat: "32.3182",
        long: "86.9023"
    }
];

Try to do with forEach but, I think it's not the way, seemed returned me the error.

oldObj.forEach((el, i) => {
    console.log(el);
});

TypeError: oldObj.forEach is not a function

Any help?

Upvotes: 9

Views: 17725

Answers (4)

Saif Uddin
Saif Uddin

Reputation: 187

const oldObj = {
    Georgia : {
        notes: "lorem ipsum",
        lat: "32.1656",
        long: "82.9001"
    },
    Alabama : {
        notes: "lorem ipsum",
        lat: "32.3182",
        long: "86.9023"
    }
}

const desireArray = Object.keys(oldObj).map((key) => ({ name: key, ...oldObj[key] }));

Explain it

const keys = Object.keys(oldObj);
const desireArray = keys.map((key) => {
    return {
        name: key,
        notes: oldObj[key].notes,
        lat: oldObj[key].lat,
        long: oldObj[key].long
    }
});

Upvotes: 1

samo0ha
samo0ha

Reputation: 3788

const oldObj = {
    Georgia : {
        notes: "lorem ipsum",
        lat: "32.1656",
        long: "82.9001"
    },
    Alabama : {
        notes: "lorem ipsum",
        lat: "32.3182",
        long: "86.9023"
    }
};

function convertObjToArr(obj) {
  let result = [];
  for(let key in obj) {
    result.push({name: key, ...obj[key]});
  }
  return result;
}

console.log(convertObjToArr(oldObj));

or try other simple solution

 return Object.keys(obj).map(item => ( {name: item, ...obj[item]} ));

Upvotes: 0

Kunal Mukherjee
Kunal Mukherjee

Reputation: 5853

  1. Find the entries using Object.entries
  2. Reduce by destructuring the current object by adding the key into the accumulator
  3. Push into the result array

const oldObj = {
    Georgia : {
        notes: "lorem ipsum",
        lat: "32.1656",
        long: "82.9001"
    },
    Alabama : {
        notes: "lorem ipsum",
        lat: "32.3182",
        long: "86.9023"
    }
};

const result = Object.entries(oldObj).reduce((acc, curr) => {
    const [key, val] = curr;
    
    acc.push({
        name: key,
        ...val
    });
    return acc;
}, []);

console.log(result);

Upvotes: 1

hgb123
hgb123

Reputation: 14871

forEach is method for array, meanwhile your oldObj is object

First you have to transform it to array, here we could do is transforming object to array of key-values pairs

And using with map could make code shorter

const oldObj = {
  Georgia: {
    notes: "lorem ipsum",
    lat: "32.1656",
    long: "82.9001",
  },
  Alabama: {
    notes: "lorem ipsum",
    lat: "32.3182",
    long: "86.9023",
  },
}

const res = Object.entries(oldObj).map(([name, obj]) => ({ name, ...obj }))

console.log(res)

References

Object.entries()

Upvotes: 12

Related Questions