-1

I need to concate two key because i want to concate first and last name in one key username. Example array of json

arr = [
 {id: 1 , first_name: Test , last_name: Test2 , city: "Berlin"  } ,
 {id: 2 , first_name: Alan, last_name: Test5 , city: "Minsk" } ,
]

I need create new object or same , to create anythink as

arr = [
 { id: 1 , first_name: Test , last_name: Test2 , city: "Berlin", username: "Test Test2" } , 
 { id: 1 , first_name: Alan, last_name: Test5 , city: "Berlin", username: "Alan Test5" } ,
]
5
  • have you tried anything? what does not work? Commented Sep 18, 2020 at 7:59
  • Why is idand city same on both on the output? Commented Sep 18, 2020 at 8:00
  • are Test and Test2 and Alan and Test5 some variables you have defined somewhere? Or should they be strings as well Commented Sep 18, 2020 at 8:01
  • I try with object.assign no work. This is only my examples of my code. Sample. Test and test 2 in same array Commented Sep 18, 2020 at 8:03
  • You can use reduce method. For your code, I think this would work - arr.reduce((accumulator, currentValue) => { currentValue.username = `${currentValue.first_name} ${currentValue.last_name}; accumulator.push(currentValue); return accumulator; }, []); But as I can see first_name and last_name are not of string type. Please note that this will work only when first_name and last_name are of string type. Commented Sep 18, 2020 at 8:17

5 Answers 5

0

const arr = [
 {id: 1 , first_name: 'Test' , last_name: 'Test2', city: "Berlin"  } ,
 {id: 2 , first_name: 'Alan', last_name: 'Test5' , city: "Minsk" } ,
]; 

arr.forEach(item => {
    item.username = `${item.first_name} ${item.last_name}`; 
})

console.log(arr); 

You can do it with a foreach loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that the provided array is not valid, i changed first and last name to be a string in order for the example to work.
0

You could use .map

const arr = [
  { id: 1, first_name: 'Test', last_name: 'Test2', city: 'Berlin' },
  { id: 2, first_name: 'Alan', last_name: 'Test5', city: 'Minsk' }
]

const res = arr.map(el => ({
  ...el,
  username: [el.first_name, el.last_name].join(' ')
}))

console.log(res)

3 Comments

@JaromandaX where exactly OP stated that he needs an in-place change?
@SebastianKaczmarek I also don't know how he interpreted that..
arr = existing array .... arr = added elements ... see how arr is the same variable
0

At first, the keys first_name and last_name has to be Strings, do they?

 let arr = [
                { id: 1, first_name: "Test", last_name: "Test2", city: "Berlin" },
                { id: 2, first_name: "Alan", last_name: "Test5", city: "Minsk" },
            ]

You can simply iterate through your array using forEach or for loop.

Then you can define a new key to your object.

 arr.forEach(element => {
    element['username'] = element.first_name + ' ' + element.last_name;
})

In practice it is better to check if the keys first_name and last_name exist, using element.hasOwnProperty('last_name').

8 Comments

element['username']?
Adds the key username to the object. It works, try it.
yes ... it does ... so does element.username
@JaromandaX element['username'] and element.username are both valid; personal preferences in coding style should not constitute a reason to downvote a correct answer.
There is no reason to do element['username'] over element.username. Using dot is a lot cleaner and less confusing. (Most IDEs warn about unnecessary use of [] notation)
|
0

If you want to create a new array you should use Array#map instead of Array#forEach. You should also deep-copy all the inner objects (an easy way is Object.parse(Object.stringify(oldObject)).

I've also assumed that the values of the first_name, last_name properties are strings (didn't have quotes on question) and that the id and city don't get merged.

var arr = [{
    id: 1,
    first_name: "Test",
    last_name: "Test2",
    city: "Berlin"
  },
  {
    id: 2,
    first_name: "Alan",
    last_name: "Test5",
    city: "Minsk"
  },
];

var newArr = arr.map(function(item) {
  var newItem = JSON.parse(JSON.stringify(item));
  newItem.username = newItem.first_name + " " + newItem.last_name;
  return newItem;
});

console.log(newArr);

2 Comments

why newArr? OP want's same arr
@JaromandaX The question says I need create new object and or same which sounds like it's a backup.
0

You can just use forEach to add the username property to each object in that array:

let ary = [
 {id: 1 , first_name: "Test" , last_name: "Test2" , city: "Berlin"  } ,
 {id: 2 , first_name: "Alan", last_name: "Test5" , city: "Minsk" }
];

ary.forEach((obj)=>
{
  obj.username = `${obj.first_name} ${obj.last_name}`;
});

console.log(ary);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.