Arshad Mehmood

Search the blog

    Today I Learned

    Add an attribute to a json object using spread operator

    javascript

    The Spread Operator is ”…” three dots, which allows to extend an object.

    Code for adding attributes to an existing json object:

    var parts = ['shoulders', 'knees'];
    var lyrics = ['head', ...parts, 'and', 'toes']; // ["head", "shoulders", "knees", "and", "toes"]

    You can also extend the same object also:

    parts =  [...parts, 'and', 'toes'];