3

An API i call gives out a result like so:

{
  "name1" : "value1",
  "name2" : "value2",
  "name3" : "value3",
  "name4" : "value4",
}

I want to change convert that to

[
  {"name1" : "value1"},
  {"name2" : "value2"},
  {"name3" : "value3"},
  {"name4" : "value4"},
[

I tried to use

Object.keys(result).forEach(function(key) {
      console.log(key, result[key]);
      let item = key;
      policyStatusList.push(
          new Object({
          item: result[key]
          })
     );
});

but it ended up as

[
  {"item" : "value1"},
  {"item" : "value2"},
  {"item" : "value3"},
  {"item" : "value4"},
[

Any suggestions?

4
  • 1
    use policyStatusList.push({[key]: result[key]}) , and no need of item = key else everything is fine. In you code item will be taken as string not as a varibale if you want to pass the variable as key in an object you need to use [key]; Commented Mar 29, 2019 at 7:51
  • No, you dont want that. Commented Mar 29, 2019 at 7:55
  • @AZ_ so apparently i just needed to add brackets to item thank you; thanks for the help everyone, will accept CertainPerformance's answer though since it's a lot more efficient than my current code Commented Mar 29, 2019 at 8:01
  • @RaphaelEstrada yes your code is not efficient, just pointing out whats wrong. Commented Mar 29, 2019 at 8:04

5 Answers 5

7

.map the Object.entries of the object:

const obj = { "name1" : "value1", "name2" : "value2", "name3" : "value3", "name4" : "value4" };
const arrOfObjs = Object.entries(obj).map(([key, val]) => ({ [key]: val }));
console.log(arrOfObjs);

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

Comments

2

You could use reduce and concat methods.

var obj = { "name1" : "value1", "name2" : "value2", "name3" : "value3", "name4" : "value4" }
var res = Object.keys(obj).reduce((result, item) => result.concat([{[item] : obj[item]}]), []);
console.log(res);

Comments

0

Try it:

const obj = {
  "name1" : "value1", 
  "name2" : "value2", 
  "name3" : "value3", 
  "name4" : "value4" 
};
const convertedArray = Object.entries(obj).map(([key, val]) => ({
    [key]: val 
}));
console.log(convertedArray);

Comments

0

I want to change convert that to ...

No, you probably don't want that. The datastructure returned by the API is great, because

1) You can easily get the value for a certain key, e.g. obj.name1

2) You can easily get all keys (Object.keys(obj)), values (Object.values(obj)) and key-value pairs (Object.entries(obj)) to iterate over.

With the datastructure that you "want" to get you can neither:

1) look up a value, as you would've to iterate the array and search for the key in each object, which is really slow & ugly.

2) Iterate over keys, values and key-value pairs, as you would've to extract the key & value out of each object, which is complicated.

So no, you don't want that.

Comments

0

Your issue is you're creating your new object with the key item - you want to use the key [item] (the value of the variable named item, not the literal string item):

const result = {
  "name1" : "value1", 
  "name2" : "value2", 
  "name3" : "value3", 
  "name4" : "value4" 
};

let policyStatusList = [];

Object.keys(result).forEach(function(key) {
  let item = key;
  policyStatusList.push(
    new Object({
      [key]: result[key]
    })
  );
});

console.log(policyStatusList);

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.