1

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);
3
  • do you want to change the existing objects or a new array with new objects? Commented Sep 24, 2018 at 12:51
  • Maybe map isn't the method you want Commented Sep 24, 2018 at 12:51
  • i need new array or whatever but output should like above key must be change in efficient way Commented Sep 24, 2018 at 12:53

7 Answers 7

2

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

persons = persons.map((x) => {
  return {
    fn : x.firstname,
    lastname : x.lastname
  }
})
Sign up to request clarification or add additional context in comments.

Comments

2

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; }

Comments

1

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);

Comments

1

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.

Comments

1

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);

Comments

1

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);

Comments

1

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);

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.