Home > Software design >  How can I extract the values ​from an object into an array while getting rid of multiple arrays in R
How can I extract the values ​from an object into an array while getting rid of multiple arrays in R

Time:10-14

I have an object called copy selected Places. At this time, I want to make the object in the form of an array.

So when I used Object.entries, the value I wanted did not come out. How do I fix my code?

this is my code

      const copyselectedPlaces = {
            A: true
            B : true
            C: true
            D :false
       }

i used Object.entries

     const arraypickplaces = Object.entries(copyselectedPlaces);
    arraypickplaces: [
        ['A', true]
        ['B', true]
        ['C', true]
        ['D', false]
        ]

expected answer

      arraypickplaces: [
        A: true,
        B : true
        C: true
        D :false
          ]

CodePudding user response:

You probably wanted to get an array of objects which you could achieve in this way:

const copySelectedPlaces = {
  A: true,
  B: true,
  C: true,
  D: false,
};

function objectToArrayOfObjects (obj) {
  return Object.entries(obj)
    .map(([k, v]) => ({[k]: v}));
}

console.log(objectToArrayOfObjects(copySelectedPlaces));

  • Related