将一个数组中的值添加到另一个数组

问题描述 投票:0回答:5

我在纯 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-)', '>:-)',  ';(', ':-*'
];

我想将一个数组中的值相加以获得以下结果,但我不明白该怎么做:

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

你知道该怎么做吗?

javascript ckeditor4.x
5个回答
3
投票

与其获取具有一个元素的对象数组,不如获取一个对象,这会很棒。

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


2
投票

您的预期结果在语法上是错误的。

如果您想要一组对象(作为一个对象的键和另一个对象的值),那么您可以尝试使用

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 = smiley_descriptions.map((k,i) => ({[k]: smiley_textual_descriptions[i]}));

console.log(my_new_array);

更新:根据要求的更正结果。您可以使用

Object.assign()
、展开语法和
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);


2
投票

您也可以使用 Array.reduce 来实现。

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


0
投票

Array.reduce() 与索引一起使用。

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


0
投票

您可以使用

.map()
Object.fromEntries()
的组合将
smiley_descriptions
数组映射到
[[key, value], ...]
对条目数组,其中每个
value
是来自其他数组的关联值相同的索引。然后,您可以使用此键值对条目数组来使用 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);

© www.soinside.com 2019 - 2024. All rights reserved.