英文:
Failed to generate dynamic key in object in Javascript
问题
我已经初始化了一个空对象,在其中动态创建一个键并在循环中插入值。但问题在于它只接受最后一个值。
为什么会这样,这里需要进行任何更正。
const obj = {};
const filteredArr = {
'Asia': ['India', 'China', 'Pakistan'],
'Europe': ['Germany']
}
const uniqCountry = ['Italy', 'Japan', 'Russia', 'Poland'];
for (const key in filteredArr) {
const values = filteredArr[key];
for (const value of values) {
if (uniqCountry.includes(value)) {
if (obj.hasOwnProperty(key)) {
obj[key].push(value);
} else {
obj['Rest'] = [];
obj['Rest'].push(value);
console.log('rest', obj['Rest']);
}
}
}
}
console.log('Object', obj);
输出:
{
'Rest': ['Italy', 'Japan', 'Russia', 'Poland']
}
英文:
I have initialized an empty object in which I am dynamically creating a key and inserting values into it inside for loop.But here issue is its only taking the last value.
Why is it happening so, any correction do I need here.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const obj = {};
const filteredArr = {
'Asia': ['India', 'China', 'Pakistan'],
'Europe': ['Germany']
}
const uniqCountry = ['Italy', 'Japan', 'Russia', 'Poland'];
for (const key in filteredArr) {
const values = filteredArr[key];
for (const value of values) {
if (uniqCountry.includes(value)) {
if (obj.hasOwnProperty(key)) {
obj[key].push(value);
} else {
obj['Rest'] = [];
obj['Rest'].push(value);
console.log('rest', obj['Rest']);
}
}
}
}
console.log('Object', obj);
<!-- end snippet -->
Output:
{}
Expected Output:
{
'Rest': ['Italy', 'Japan', 'Russia', 'Poland']
}
答案1
得分: 1
问题不是很清楚,但这显示了一些类似于您所要求的内容。我已将'Italy'添加到filteredArr,并将'India'添加到uniqCountry,以展示这些示例。
const obj = {};
const filteredArr = {
'亚洲': ['印度', '中国', '巴基斯坦'],
'欧洲': ['德国', '意大利']
}
let uniqCountry = ['意大利', '日本', '俄罗斯', '波兰', '印度'];
for (const key in filteredArr) {
const values = filteredArr[key];
for (const value of values) {
if (uniqCountry.includes(value)) {
if (!obj.hasOwnProperty(key)) {
obj[key] = [...(obj[key] ?? []), value];
uniqCountry = uniqCountry.filter(u => u !== value);
}
}
}
}
obj['其他'] = uniqCountry;
console.log('对象', obj);
请注意,我将'Asia'翻译为'亚洲','Europe'翻译为'欧洲','Rest'翻译为'其他'。如果您有其他需要,请告诉我。
英文:
The question is not so clear but this shows something like you asked for. I did added 'Italy' to the filteredArr and 'India' to the uniqCountry to show those examples as well.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const obj = {};
const filteredArr = {
'Asia': ['India', 'China', 'Pakistan'],
'Europe': ['Germany', 'Italy']
}
let uniqCountry = ['Italy', 'Japan', 'Russia', 'Poland', 'India'];
for (const key in filteredArr) {
const values = filteredArr[key];
for (const value of values) {
if (uniqCountry.includes(value)) {
if (!obj.hasOwnProperty(key)) {
obj[key] = [...(obj[key] ?? []), value];
uniqCountry = uniqCountry.filter(u => u !== value);
}
}
}
}
obj['Rest'] = uniqCountry;
console.log('Object', obj);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论