在JavaScript中无法生成对象中的动态键。

huangapple go评论77阅读模式
英文:

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 = {
  &#39;Asia&#39;: [&#39;India&#39;, &#39;China&#39;, &#39;Pakistan&#39;],
  &#39;Europe&#39;: [&#39;Germany&#39;]
}

const uniqCountry = [&#39;Italy&#39;, &#39;Japan&#39;, &#39;Russia&#39;, &#39;Poland&#39;];

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[&#39;Rest&#39;] = [];
        obj[&#39;Rest&#39;].push(value);
        console.log(&#39;rest&#39;, obj[&#39;Rest&#39;]);
      }
    }
  }
}
console.log(&#39;Object&#39;, obj);

<!-- end snippet -->

Output:

{}

Expected Output:

{
  &#39;Rest&#39;: [&#39;Italy&#39;, &#39;Japan&#39;, &#39;Russia&#39;, &#39;Poland&#39;]
}

答案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 = {
  &#39;Asia&#39;: [&#39;India&#39;, &#39;China&#39;, &#39;Pakistan&#39;],
  &#39;Europe&#39;: [&#39;Germany&#39;, &#39;Italy&#39;]
}

let uniqCountry = [&#39;Italy&#39;, &#39;Japan&#39;, &#39;Russia&#39;, &#39;Poland&#39;, &#39;India&#39;];

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 =&gt; u !== value);
      }
    }
  }
}
obj[&#39;Rest&#39;] = uniqCountry;
console.log(&#39;Object&#39;, obj);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 17:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定