【问题标题】:Javascript get unique value of an indented jsonJavascript获取缩进json的唯一值
【发布时间】:2021-04-14 09:26:53
【问题描述】:

我的 JSON array of objects 具有以下结构:

obj: [
    {   
        itemTitle: 'title-a',
        itemTags: [
            { tag: 'tag-a' },
            { tag: 'tag-b' }
        ]                       
    },
    {   
        itemTitle: 'title-b',
        itemTags: [
            { tag: 'tag-c' }
        ]                       
    },
    {   
        itemTitle: 'title-c',
        itemTags: [
            { tag: 'tag-c' },
            { tag: 'tag-b' }
        ]                       
    }
]

我需要在这样的数组中提取不同的标签值[tag-a, tag-b, tag-c] 我会尝试这种方法:

const tagsArray = obj.map(elem => elem.itemTags);

var tags = [];
for (var i = 0; i < tagsArray.length; i++) {
    tags.push(tagsArray[i].map(item => item.tag))
}
//tagsArray[0]: [{"tag":"tag-a"},{"tag":"tag-b"}]
//tagsArray[1]: [{"tag":"tag-c"}]
//tagsArray[2]: [{"tag":"tag-c"},{"tag":"tag-b"}]

//tags: tag-a,tag-b,tag-c,tag-c,tag-b

const unique = [...new Set(tags)];
//unique: tag-a,tag-b,tag-c,tag-c,tag-b

它不返回不同的值

【问题讨论】:

  • “我有 json 对象数组...” - 不,obj(该内容的名称很糟糕)是一个对象数组。跨度>
  • Array.prototype.flatMap()
  • @Andreas 非常感谢。

标签: javascript arrays unique


【解决方案1】:

您可以尝试在与flatMap()map() 相同的循环中获取标签

var obj = [
    {   
        itemTitle: 'title-a',
        itemTags: [
            { tag: 'tag-a' },
            { tag: 'tag-b' }
        ]                       
    },
    {   
        itemTitle: 'title-b',
        itemTags: [
            { tag: 'tag-c' }
        ]                       
    },
    {   
        itemTitle: 'title-c',
        itemTags: [
            { tag: 'tag-c' },
            { tag: 'tag-b' }
        ]                       
    }
]
const tagsArray = obj.flatMap(elem => elem.itemTags.map(i=>i.tag));

const unique = [...new Set(tagsArray)];
console.log(unique)

【讨论】:

    【解决方案2】:

    问题

    您正在将数组推送到tagsArray 中的每个项目中。内容看起来像

    [["tag-a","tag-b"],["tag-c"],["tag-c","tag-b"]]
    

    当你需要时

    ["tag-a","tag-b","tag-c","tag-c","tag-b"]
    

    解决方案

    ==> 使用concat 代替push

    const obj = [{itemTitle:'title-a',itemTags:[{tag:'tag-a'},{tag:'tag-b'}]},{itemTitle:'title-b',itemTags:[{tag:'tag-c'}]},{itemTitle:'title-c',itemTags:[{tag:'tag-c'},{tag:'tag-b'}]}];
    
    var tags = [];
    const tagsArray = obj.map(elem => elem.itemTags);
    for (var i = 0; i < tagsArray.length; i++)
        tags = tags.concat(tagsArray[i].map(item => item.tag)); // Use concat instead of push
    const unique = [...new Set(tags)];
    console.log(unique);

    重构代码

    您可以使用Array#flatMap

    const obj = [{itemTitle:'title-a',itemTags:[{tag:'tag-a'},{tag:'tag-b'}]},{itemTitle:'title-b',itemTags:[{tag:'tag-c'}]},{itemTitle:'title-c',itemTags:[{tag:'tag-c'},{tag:'tag-b'}]}];
    
    const tagsArray = obj.flatMap(({itemTags}) => itemTags.map(({tag}) => tag));
    console.log([...new Set(tagsArray )]);

    flatMap() 方法返回一个新数组 回调函数到数组的每个元素,然后展平 结果提升一级


    【讨论】:

    【解决方案3】:
    let t=arr.flatMap(item=>item.itemTags.flatMap(tag=>Object.values(tag)))
    u = new Set([...t])
    
    

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 2019-06-21
      • 1970-01-01
      • 2012-09-16
      相关资源
      最近更新 更多