0

I already have a existing array of objects like this,

 details = [
    {
        'asjad': {
            'age': 23,
            'area': 'permiet'
        }
    }

I have a function like this,

function addValues(_arr,_name,_age){

    _arr.push({
        _name : {
            age : _age
        }
    })

}


addValues(details,'foo',24);

Where I try to add values to a array of objects by passing the values of the object to be added.

But the expected output is not foo, but instead it the key is getting added as _name

1
  • 1
    use [_name] instead of _name in the arr.push Commented Oct 7, 2018 at 7:51

5 Answers 5

1

Just change the above code to this. You will achieve your need.

function addValues(_arr,_name,_age){

    _arr.push({
        [_name] : {
            age : _age
        }
    })

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

Comments

1

If you define an object like this in JavaScript, the key will always be a string – in your case a _name. To assign a dynamic variable as a key to an object, you'll need to use the bracket notation []. The value, however, is evaluated as an expression. In your case, _age is evaluated, because it's a value and not a key.

You could easily fix with e.g. this example:

function addValues(_arr, _name, _age){

    var object = {};
    object[_name] = {
        age: _age
    };
    _arr.push(object);

}

These are many ways to declare an object in JavaScript:

Inline Declaration:

var object = {
   _name: '123'
};

_name will always be a constant string.

Dot notation:

var object = {};
var object._name = '123';

→ In the dot notation, _name will also always be a constant string.

Bracket Notation:

var object = {};
var object[_name] = '123';

→ In the bracket notation, _name is an evaluated expression. In this case, a variable name _name. You could also pass a string by using object['_name'].

Computed property names:

As the other answers have pointed out, with ES6 (a newer version of JavaScript, which is not fully supported by all browsers, but can be transpiled to ES5), you could also use computed property names:

var object = {
   [_name]: '123'
};

→ In this notation, the _name will be evaluated and its value will be the key in this object. Keep in mind, that you won't have IE support without transpiling this snippet.

Comments

0
function addValues(_arr,_name,_age){

    var newItem = {};
    newItem[_name] = {};
    newItem[_name]['age'] = _age;

    _arr.push(newItem);
}

1 Comment

write some text to explain your answer.
0
function addValues(_arr,_name,_age){

    _arr.push({
        [_name] : {
            age : _age
        }
    })

}


addValues(details,'foo',24);

1 Comment

Please take the time to explain your answer for the OP and those how may come across it in the future.
0

_arr.push({ [_name] : { age : _age } })

Create key value pair json you need to start key with [] for key interpolation

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.