简体   繁体   中英

Add values from an array to another array

I have the following two arrays in pure JavaScript:

var smiley_descriptions = [
  'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise',
  'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss'
];
var smiley_textual_descriptions = [
  ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|',  '>:(', 'o:)',  
  '8-)', '>:-)',  ';(', ':-*'
];

I want to add the values from one array to obtain the following result, but I don't understand how to do it:

var my_new_array = {smiley: ':)', sad: ':(', wink: ';)', laugh: ':D', cheeky: ':P', blush: ':*)', surprise: ':-o', indecision: ':|', angry: '>:(', angel: 'o:)', cool: '8-)', devil: '>:-)', crying: ';(', kiss: ':-*'};

Do you have any idea how to do it?

Instead of getting an array of an object that has one element, it will be great to get an object instead.

 var smiley_descriptions = [ 'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise', 'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss' ]; var smiley_textual_descriptions = [ ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|', '>:(', 'o:)', '8-)', '>:-)', ';(', ':-*' ]; const result = {}; smiley_descriptions.forEach((item, index) => { result[item] = smiley_textual_descriptions[index]; }) console.log(result);

Your intended result is syntactically wrong.

If you want an array of objects (as key from one and the value from other) then you can try using map() like the following way:

 var smiley_descriptions = [ 'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise', 'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss' ]; var smiley_textual_descriptions = [ ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|', '>:(', 'o:)', '8-)', '>:-)', ';(', ':-*' ]; var my_new_array = smiley_descriptions.map((k,i) => ({[k]: smiley_textual_descriptions[i]})); console.log(my_new_array);

Update: according to the required corrected result. You can use Object.assign() , spread syntax and map() :

 var smiley_descriptions = [ 'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise', 'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss' ]; var smiley_textual_descriptions = [ ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|', '>:(', 'o:)', '8-)', '>:-)', ';(', ':-*' ]; var my_new_array = Object.assign(...smiley_descriptions.map((k, i) => ({[k]: smiley_textual_descriptions[i]}))); console.log(my_new_array);

You can also use Array.reduce to implement this.

 var smiley_descriptions = [ 'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise', 'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss' ]; var smiley_textual_descriptions = [ ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|', '>:(', 'o:)', '8-)', '>:-)', ';(', ':-*' ]; const result = smiley_descriptions.reduce( (val, cur, index) => ({...val, [cur]: smiley_textual_descriptions[index] }), {} ); console.log(result);

Use Array.reduce() with index.

 var smiley_descriptions = [ 'smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise', 'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss' ]; var smiley_textual_descriptions = [ ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|', '>:(', 'o:)', '8-)', '>:-)', ';(', ':-*' ]; const my_new_array = smiley_descriptions.reduce((acc, cur, index) => { acc[cur] = smiley_textual_descriptions[index]; return acc; }, {}); console.log(my_new_array);

You could use a combination of .map() and Object.fromEntries() to map your smiley_descriptions array to an array of [[key, value], ...] pair entries, where each value is an associated value from your other array at the same index. You can then use this key-value pair entry array to build an object using Object.fromEntries:

 const smiley_descriptions = ['smiley', 'sad', 'wink', 'laugh', 'cheeky', 'blush', 'surprise', 'indecision', 'angry', 'angel', 'cool', 'devil', 'crying', 'kiss' ]; const smiley_textual_descriptions = [ ':)', ':(', ';)', ':D', ':P', ':*)', ':-o', ':|', '>:(', 'o:)', '8-)', '>:-)', ';(', ':-*' ]; const res = Object.fromEntries(smiley_descriptions.map( (desc, i) => [desc, smiley_textual_descriptions[i]]) ); console.log(res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM