我有2个数组,我想组合两个数组,这将导致第三个数组,就像在第二个图像中一样。我怎么能这样做?
const mapper = {
characters: {
'onepiece': {
'luffy': 'pirate king',
'sanji': 'cook'
},
'deathnote': {
'ryuk': 'shinigami',
'lee': 'weirdo :)'
}
},
animes: {
'onepiece': 'one piece',
'deathnote': 'death note'
}
}

我想得到这样的结果:
'路飞:海贼王海贼王',
' Sanji:海贼王烹制',
' Ryuk:死亡笔记中的Shinigami',
' Lee:Weirdo :) in Death Note'
答案 0 :(得分:0)
Javascript实施
const mapper = {
characters: {
'onepiece': {
'luffy': 'pirate king',
'sanji': 'cook',
},
'deathnote': {
'ryuk': 'shinigami',
'lee': 'weirdo :)',
},
},
animes: {
'onepiece': 'one piece',
'deathnote': 'death note',
},
};
const result = Object.assign({}, mapper.characters.onepiece, mapper.characters.deathnote);
console.log(result);
答案 1 :(得分:0)
您可以为此嵌套经典 for
/ in
循环。
const mapper = {
characters: {
'onepiece': {
'luffy': 'pirate king',
'sanji': 'cook'
},
'deathnote': {
'ryuk': 'shinigami',
'lee': 'weirdo :)'
}
},
animes: {
'onepiece': 'one piece',
'deathnote': 'death note'
}
};
let result = [];
for (var key in mapper.characters) {
for (var key2 in mapper.characters[key]) {
result.push(jsUcfirst(key2) + " : " + jsUcfirst(mapper.characters[key][key2]) + " in " + jsUcfirst(mapper.animes[key]));
}
}
//Capitalize first letter of each word on a string
function jsUcfirst(string) {
let arr = [];
string = string.split(" ");
for (var key in string) arr.push(string[key].charAt(0).toUpperCase() + string[key].slice(1));
return arr.join(" ");
}
console.log(result);