javascriptckeditor4.x

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 ?


Solution

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