Reece
Reece

Reputation: 2711

How to map key and value pair to each index in object

I am trying to add a new key and value pair to my object using map. The value is simply an empty array so that I can push objects into it later.

Here's my code:

let columns = [
    {
        _id: "5e8af4591c9d440000a1a4ed",
        column_name: "Opportunities",
        column_value: 0,
        column_percentage: 0.25
    },
    {
        _id: "5e8af4a81c9d440000a1a4ee",
        column_name: "Prospects",
        column_value: 0,
        column_percentage: 0.5
    }
];

let output = columns.map(() => ({posts: []}));

console.log(output)

here's what I want my object to look like after mapping:

let columns = [
    {
        _id: "5e8af4591c9d440000a1a4ed",
        column_name: "Opportunities",
        column_value: 0,
        column_percentage: 0.25,
        posts: []
    },
    {
        _id: "5e8af4a81c9d440000a1a4ee",
        column_name: "Prospects",
        column_value: 0,
        column_percentage: 0.5,
        posts: []
    }
];

But the current output is:

[
    {
        posts: []
    },
    {
        posts: []
    }
]

Upvotes: 0

Views: 1222

Answers (3)

FZs
FZs

Reputation: 18619

Your code doesn't work because the new array will contain exactly what the mapper function returns.

Even if it has nothing to do with the original array's contents, it will leave its return value intact.

So, if you return an object {posts:[]}, it will be the output.

You have to merge that object into the current array element inside the mapper function.


You can use the object spread syntax to merge two objects:

let columns = [
    {
        _id: "5e8af4591c9d440000a1a4ed",
        column_name: "Opportunities",
        column_value: 0,
        column_percentage: 0.25
    },
    {
        _id: "5e8af4a81c9d440000a1a4ee",
        column_name: "Prospects",
        column_value: 0,
        column_percentage: 0.5
    }
];

columns.map(obj => ({...obj, posts: []}));

However, that's a relatively new feature, so, if you need greater support, you can use Object.assign:

let columns = [
    {
        _id: "5e8af4591c9d440000a1a4ed",
        column_name: "Opportunities",
        column_value: 0,
        column_percentage: 0.25
    },
    {
        _id: "5e8af4a81c9d440000a1a4ee",
        column_name: "Prospects",
        column_value: 0,
        column_percentage: 0.5
    }
];

columns.map(obj => Object.assign({posts: []}, obj));

Upvotes: 2

Mechanic
Mechanic

Reputation: 5380

using forEach();

columns.forEach(el => el.posts=[] )

let columns = [
    {
        _id: "5e8af4591c9d440000a1a4ed",
        column_name: "Opportunities",
        column_value: 0,
        column_percentage: 0.25
    },
    {
        _id: "5e8af4a81c9d440000a1a4ee",
        column_name: "Prospects",
        column_value: 0,
        column_percentage: 0.5
    }
];
columns.forEach(el => el.posts=[] )

console.log(columns)

Upvotes: 0

Camelia
Camelia

Reputation: 5

You can try:

let newColumns = columns.map((item) => Object.assign({}, item, {posts: []});  
console.log(newColumns);

Upvotes: 0

Related Questions