avatar
modify object property in an array of objects Javascript

• Be assumption that we have JavaScript array that contains a collection of objects.

let a = [
    {
        "id": 21,
        "name": "Programming Language",
        "level": 2,
        "slug": "programming-language",
        "thumbnail": []
    },
    {
        "id": 22,
        "name": "Framework",
        "level": 2,
        "slug": "framework",
        "thumbnail": []
    }
];

» Iterate through the array and modify using for-loop.

for (let i = 0; i < a.length; i++) {
    a[i].slug = a[i].slug + '/sample';
}

» Create new array with .map().

const modifiedArray = a.map(item => ({
    ...item,
    slug: item.slug + '/sample'
}));

» Use forEach to modify the original array

a.forEach(item => {
    item.slug_en = item.slug_en + '/sample';
});
You need to login to do this manipulation!