1

Here my initial object :

partners {
  0: "Proxy"
  2: "Skate"
  8: "Air"
}

I want to have this :

partners [
  0:{name: "Proxy"}
  1:{name: "Skate"}
  2:{name: "Air"}
]

I tried this without success :

var newArray = Object.values(this.initialObject).map(function (value) {
                                return { ['name']: obj[value] };
                            });

Thank you very much.

4
  • And the question/problem is? -> How do I ask a good question? Commented Jan 22, 2020 at 11:18
  • 4
    Return value should be return {name: value} Commented Jan 22, 2020 at 11:19
  • Always check the error console. Commented Jan 22, 2020 at 11:22
  • 3
    Object.values(partners).map(name => ({name})) Commented Jan 22, 2020 at 11:25

8 Answers 8

3

name does not need to be wrapped in [' '] as it is a key nor do you need to use obj to get the value as value is the variable passed by the function.

Try this instead:

var partners = {
    0: "Proxy",
    2: "Skate",
    8: "Air"
}

var newArray = Object.values(partners).map(function (value) {
    return {name: value};
});

console.log(newArray)

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

Comments

3

const a = {
  0: "Proxy",
  2: "Skate",
  8: "Air"
}

const result = Object.values(a).map((value) => {
    return { name: value };
});
console.log(result)

This will do it, you had to replace the obj[value] with value and replace ['name'] with simple name.

Comments

3
partners ={
  0: "Proxy"
  2: "Skate"
  8: "Air"
} 

var newpartners=[];
    Object.keys(partners).map(item=>{
      newpartners.push({name:partners[item]})
    })

console.log(newpartners);

Comments

2

Yo can use the following methods code

var partners = {
    0: "Proxy",
    2: "Skate",
    8: "Air"
}
var arr = [];
var keys = Object.keys(partners);
var values = Object.values(partners);

keys.forEach((ele, indx) => {
    arr.push({ "name": values[indx] });
})

console.log(arr);

var partners = {
    0: "Proxy",
    2: "Skate",
    8: "Air"
}

var newArray = Object.values(partners).map(function (value) {
    return { name : value };
});

console.log(newArray);

Comments

1

You should use the param name from the handler.

Object.values(this.initialObject).map(name => ({name}));

Comments

1

Try This :

 var tempPartners=[];
    for (x in partners) {
    var obj=JSON.parse(`{"name":"`+partners[x]+`"}`);
    tempPartners.push(obj);
    }
console.log(tempPartners);

The output will be like

 0: {name: "Proxy"}
    1: {name: "Skate"}
    2: {name: "Air"}

Comments

1

You can try something like this, just using an array as a container for the final desired output, and loop over the original object with for ... in loop. Hope this helps. :)

var p = {
  "0": "Proxy",
  "2": "Skate",
  "8": "Air"
}
var a = [];
for(var key in p){
 if (p.hasOwnProperty(key)) {
   a.push({name: p[key]})
  }

}
console.log(a)

Another way to loop through an object is to use Object.entries method, This method return an array of arrays, each array contain the key and the value

const fruits = {
  apple: 28,
  orange: 17,
  pear: 54,
}

const entries = Object.entries(fruits)
console.log(entries)
// [
//   [apple, 28],
//   [orange, 17],
//   [pear, 54]
// ]

So, in your case you can try something like this :

const entries = Object.entries(p);
var a = [];
for (const [key, value] of entries) {

  a.push({name: value})
}

1 Comment

You should describe your code to make it more effective long term.
0

You can do something like this

var partners = {"Proxy", "Skate","Air" };
var newArray = [];
for(var i = 0; i<=partners.length; i++){
    newArray.push({Name: element[i]});
}
console.log(newArray);

1 Comment

If his initial object is this { 0: "Proxy" 2: "Skate" 8: "Air" }, how have you ended up having {"Proxy", "Skate","Air" } ?

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.