0

I have a single object such as:

{
A: false
B: false
C: false
D: false
E: false
F: false
}

I want it to be separated in key/value pair in list as:

[
    {
    A:false
    },{
    B: false
    },
    {
    C: false
    },{
    D: false
    }
]

Is it possible to change the object into different list like this?

1
  • 4
    use Object.entries and Array#map Commented Sep 23, 2019 at 10:14

4 Answers 4

4

You could map the entries and take a single entry and create an object.

var data = { A: false, B: false, C: false, D: false, E: false, F: false },
    result = Object.entries(data).map(a => Object.fromEntries([a]));

console.log(result);

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

1 Comment

2

You can use Object.entries to get an array of the form of Array<[key, value]>, which you can then map to the output you desire.

var object = { A: false, B: false, C: false, D: false, E: false, F: false };

var result = Object.entries(object).map(([i, v]) => ({ [i]: v }));

console.log(result);

Or if you want an IE compatible approach you could use Object.keys instead.

var object = { A: false, B: false, C: false, D: false, E: false, F: false };

var result = Object.keys(object).map(function(key) {
  var result = {};
  return result[key] = object[key], result;
});

console.log(result);

4 Comments

my angular 7 is not supporting
@AshwinKarki Which part doesn't it support?
@AshwinKarki I added another example that should be supported by every browser and framework.
Property 'entries' does not exist on type 'ObjectConstructor'
1

There are shorter ways, but this way makes the process very clear:

const origObject = 
{
  A: false, // Each property of an object is a "key/value pair" ('key' aka 'name'.)
  B: false,
  C: false,
  D: false,
  E: false,
  F: false
};
const newArray = []; // Defines a new Array to store our new objects in
const origObjectKeys = Object.keys(origObject); // Gets keys from `origObject` (A,B,etc)


// Loops through all the keys , visiting each key once (in no particular order)
for(let thisKey of origObjectKeys){ 
  let thisValue = origObject[thisKey]; // Gets the value stored at a particular key
  let newObject = {}; // Defines a new empty object

  // Adds a property to the object, naming it the same as the current key (egs: A, B)
  //   and stores a value in that property (same as the current value, eg: false)
  newObject[thisKey] = thisValue;

  // Adds the new object to the Array
  newArray.push(newObject);
}
// When the loop stops running, one new object has been added to the array for each key

console.log(newArray);

Comments

0

You can do something like this:

let originalObject = {
    A: false,
    B: false,
    C: false,
    D: false,
    E: false,
    F: false
};

let newObjectArray = [];
Object.keys(originalObject).forEach((key) => {
   let newObject = {}
   newObject[key] = originalObject[key];
   newObjectArray.push(newObject);
})
console.log(newObjectArray);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.