Hemanth SP
Hemanth SP

Reputation: 99

How to make map in JavaScript change the object key

I have the following input data:

var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

I need to change the value of the key in every object so expected output data is:

persons = [
    {fn : "Malcom", lastname: "Reynolds"},
    {fn : "Kaylee", lastname: "Frye"},
    {fn : "Jayne", lastname: "Cobb"}
]; 

I am using map function in JavaScript but it is not working.

persons.map(x => x.firstname=x.fn);

Upvotes: 1

Views: 272

Answers (7)

Nina Scholz
Nina Scholz

Reputation: 386624

You could use a destructuring assignment with rest parameters ... for objects. Then map the new property with the rest properties.

This proposal works only with newer JS or with babeljs.

var persons = [{ firstname : "Malcom", lastname: "Reynolds" }, { firstname : "Kaylee", lastname: "Frye" }, { firstname : "Jayne", lastname: "Cobb" }];

console.log(persons.map(({ firstname: fn, ...rest }) => ({ fn, ...rest })));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Mario
Mario

Reputation: 4998

You can also use reduce

// Given
const persons = [
    {
        firstname: "Malcom",
        lastname: "Reynolds"
    },
    {
        firstname: "Kaylee",
        lastname: "Frye"
    },
    {
        firstname: "Jayne",
        lastname: "Cobb"
    }
];

// When
const people = persons.reduce((previousValue, currentValue) => {
    previousValue.push({fn: currentValue.firstname, lastname: currentValue.lastname});

    return previousValue;
}, []);

// Then
console.log(people);

Upvotes: 1

protoproto
protoproto

Reputation: 2099

Use as follow:

var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];
var result = persons.map(current => { 
    var obj = {};
    obj.fn = current.firstname;
    obj.lastname = current.lastname;
    return obj;
  });
console.log(result);

Upvotes: 1

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Use forEach():

var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

persons.forEach((obj)=>{
  obj.fn = obj.firstname;
  delete obj.firstname;
});
console.log(persons);

Upvotes: 1

Tu Nguyen
Tu Nguyen

Reputation: 10179

Try this:

let persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

let newPersons = persons.map(person => {
    return ({
       fn: person.firstname, lastname: person.lastname
    })
})

The newPersons array will be exactly the array that you need. Btw, I'm using let instead of var.

Upvotes: 1

MysterX
MysterX

Reputation: 2368

Your thoughts was right but need to be a little bit changed:

persons = persons.map((x) => {
  return {
    fn : x.firstname,
    lastname : x.lastname
  }
})

Upvotes: 2

kockburn
kockburn

Reputation: 17616

Use map like so by returning a new object.

var persons = [
    {firstname : "Malcom", lastname: "Reynolds"},
    {firstname : "Kaylee", lastname: "Frye"},
    {firstname : "Jayne", lastname: "Cobb"}
];

const newPersons = persons.map(person=>({fn:person.firstname, ln:person.lastname}));

console.log(newPersons);

Upvotes: 1

Related Questions