2

How to add a key value to javascript object, i have an array like this : var a = [20, 21, 22];

and i tried to convert it into a javascript object using this code : var id = Object.assign({}, a);

the result is : {0 : 20, 1 : 21, 2 : 22}

but, how to change the result like an object below ? :

0 : {id : 20}, 1 : {id : 21}, 2 : {id : 22}

any help will be appreciated

1
  • {id : 20, id: 21, id:22} is equivalent to just 1 of the id. Commented Aug 22, 2017 at 5:51

4 Answers 4

3

Based on your comment "the format of the object supposed to be like this: 0 : {id : 20}, 1 : {id : 21}, 2 : {id : 22}", this looks like a job for .map():

var input = [ 20, 21, 22 ];

var output = input.map( function( value ) {
    return { id: value };
});

console.log( output );

Or, in modern JavaScript using an arrow function:

var input = [ 20, 21, 22 ];

var output = input.map( value => ({ id: value }) );

console.log( output );

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

1 Comment

Glad to help. Happy hacking! :-)
2

You can't and shouldn't have an object like {id : 20, id: 21, id:22}. Each key in the object should be unique, otherwise it beats the purpose of being a 'key'.

If you, hypothetically, had an object like

var a = {id : 20, id: 21, id:22};

,then how are you planning to access those elements? What will a.id return and how will it help you?

If you want to get 0 : {id : 20}, 1 : {id : 21}, 2 : {id : 22}, you could do this this way:

let a = [20, 21, 22];
let obj = Array();
for(let i of a) {
  obj.push({id: i});
}

1 Comment

i'm so sorry, but the format of the object supposed to be like this: 0 : {id : 20}, 1 : {id : 21}, 2 : {id : 22}
0

Create object like: var collections = [], a = [20, 21, 22];

Iterate array a([20, 21, 22]), and add individual item in collections like below which is array.

for (i = 0; i < a.length; i++) {
    collections.push({id: a[i]});
}

Cheers...

Comments

0

{id : 20, id: 21, id:22} is equivalent to just 1 key/value pair. their is no way to have {id : 20, id: 21, id:22} as an object and keep values as 20, 21, 22

You can try making it into an array like

{id : [20, 21, 22]}

or

[{id:20}, {id:21}, {id:22}]

var a = [20, 21, 22];
var result = [];

for(let i=0; i<a.length; i++){
    result.push({id: a[i]});
}

//console.log(result)
var id = Object.assign({}, result);
console.log(id);

4 Comments

have you ever heard about dustjs ? in dust, i can't render a standar array, it must be an associative array....
i tried your answer, but the console displaying : id{ 0 : 20, 1 : 21, 2 : 22}
you can create an array of objects which has objects [{id : 20}, {id : 21}, {id : 22}]
idk why, but using push() make my browser unresponsed... but thanks for trying to help me btw :)

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.