简体   繁体   English

Javascript 阵列不正确 map

[英]Javascript Array doesn't map correctly

I'm "consolling" the entire code... but I can't find any issue, only a weird behaviour.我正在“安慰”整个代码......但我找不到任何问题,只有一个奇怪的行为。

Let me explain: I've an angular component (let's call it parent ) which send some tags to his child through a inputTags array.让我解释一下:我有一个 angular 组件(我们称之为parent ),它通过inputTags数组向他的孩子发送一些标签 Then I need to set another list with ALL the tags of the user, called allTags .然后我需要设置另一个包含所有用户标签的列表,称为allTags

The array (both inputTags and allTags ) is formatted like this: { id: 'tagId', name: 'tagName' }数组( inputTagsallTags )的格式如下: { id: 'tagId', name: 'tagName' }

I need to make an unified array of those two.我需要制作这两者的统一数组。 The expected output should contain an array of items that it's formatted like this: { id: 'tagId', name: 'tagName', selected: boolean }预期的 output 应包含一个项目数组,其格式如下: { id: 'tagId', name: 'tagName', selected: boolean }

In order to do this I'm mapping the allTags array in this way:为了做到这一点,我以这种方式映射allTags数组:

Let's suppose that:让我们假设:

inputTags = [
    { id: 'work', name: 'Work' },
    { id: 'motivation', name: 'Motivation' }
];

allTags = [
    { id: 'network', name: 'Network' },
    { id: 'work', name: 'Work' },
    { id: 'smart', name: 'Smart' },
    { id: 'motivation', name: 'Motivation' }
];

Now... allTags are actually retrived from a server, so my code looks something like this:现在... allTags 实际上是从服务器检索的,所以我的代码看起来像这样:

this.tagsService.getAll().subscribe(tags => {

    this.allTags = tags.map(tag => {
        let select = false;
        this.inputTags.forEach(inputTag => { select = (inputTag.id === tag.id) })
        return {
          id: tag.id,
          name: tag.name,
          selected: select,
        };      
    });

})

This for me seems quite standard, but in fact NO, because instead of getting:这对我来说似乎很标准,但实际上不,因为不是得到:

allTags = [
    { id: 'network', name: 'Network', selected: false },
    { id: 'work', name: 'Work', selected: true }, // is selected
    { id: 'smart', name: 'Smart', selected: false },
    { id: 'motivation', name: 'Motivation', selected: true } // is selected
];

I get this:我明白了:

allTags = [
    { id: 'network', name: 'Network', selected: false },
    { id: 'work', name: 'Work', selected: false }, // is NOT selected
    { id: 'smart', name: 'Smart', selected: false },
    { id: 'motivation', name: 'Motivation', selected: true } // is selected
];

Basically the issue is that it's selecting only one tag, not multiple tags.基本上问题是它只选择一个标签,而不是多个标签。

You can try some :你可以尝试一些

this.allTags = tags.map(tag => {
    return {
      id: tag.id,
      name: tag.name,
      selected: this.inputTags.some(inputTag => inputTag.id === tag.id)
    };      
});

JavaScript Array map() Method JavaScript Array map() 方法

*)creates a new array with the results of calling a function for every array element and it calls the provided function once for each element in an array, in order. *) 使用为每个数组元素调用 function 的结果创建一个新数组,并为数组中的每个元素按顺序调用提供的 function 一次。

Note: map() Method does not execute the function for array elements without values and it does not change the original array.注意: map()方法不会对没有值的数组元素执行 function 并且不会改变原始数组。

Try the following:尝试以下操作:

this.allTags = allTags.map(tag => ({
  id: tag.id,
  name: tag.name,
  selected: inputTags.some(i => i.id === tag.id),
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM