简体   繁体   English

带有箭头功能的Javascript json过滤器

[英]Javascript json filter with arrow function

I was wondering how I can filter the values "tags" in the json above using an arrow function:我想知道如何使用箭头函数过滤上面 json 中的值“标签”:

const ttag = [{
    "code": 795302828,
    "code_integration": "123",
    "company": "ACME LTD",
    "phone": "135575788",
    "tags": [{"tag": "companyAA"},
             {"tag": "companyBB"},
             {"tag": "companyCC"},
             {"tag": "companyDD"}
            ],
    "status": "Y"
}]

const onlyTags = ttag.filter(f => f.tags)
console.log(onlyTags)

expected result: ['companyAA', 'companyBB', 'companyCC', 'companyDD']预期结果:['companyAA'、'companyBB'、'companyCC'、'companyDD']

First of all, that's not how filter method works.首先,这不是filter方法的工作原理。 Filter returns an array with values that are true for function passed as the argument. Filter返回一个数组,其值对于作为参数传递的函数为true More you can read here .您可以在此处阅读更多内容。

You could use map & flatMap , like this:您可以使用map & flatMap ,如下所示:

ttag.flatMap(f => f.tags.map(t => t.tag));

map returns array of values specified in the passed method. map返回在传递的方法中指定的值的数组。 flatMap does the same, but if the result is an array it flattens it, so result is an array of strings instead of an array of arrays of strings. flatMap做同样的事情,但如果结果是一个数组,它会将其展平,因此结果是一个字符串数组而不是一个字符串数组数组。

Important重要的

flatMap is not supported in Internet Explorer, but it's already unsupported browser so I wouldn't bother. Internet Explorer 不支持flatMap ,但它已经是不受支持的浏览器,所以我不会打扰。

You can iterate through your array and push the results to a new array, something like this您可以遍历数组并将结果推送到新数组,如下所示

 const ttag = [{ "code": 795302828, "code_integration": "123", "company": "ACME LTD", "phone": "135575788", "tags": [{"tag": "companyAA"}, {"tag": "companyBB"}, {"tag": "companyCC"}, {"tag": "companyDD"} ], "status": "Y" }] const res = []; ttag.forEach(x => { x.tags.forEach(y => { res.push(y.tag); }) }) console.log(res);

Maybe an ugly way to do it, but with the first .map you get each item of the main array, and with the second .map an array of tags inside it.这可能是一种丑陋的方法,但是使用第一个.map可以获得主数组的每个项目,而使用第二个.map可以获得其中的标签数组。

 const ttag = [{ "code": 795302828, "code_integration": "123", "company": "ACME LTD", "phone": "135575788", "tags": [{"tag": "companyAA"}, {"tag": "companyBB"}, {"tag": "companyCC"}, {"tag": "companyDD"} ], "status": "Y" }] const onlyTags = ttag.map(f => f.tags.map(t => t.tag)); console.log(onlyTags); console.log(onlyTags[0]); // expected output

Here's one solution using map and reduce.这是使用 map 和 reduce 的一种解决方案。 At the end the array is flatten so it returns only an array containing the city names最后,数组被展平,因此它只返回一个包含城市名称的数组

 const ttag = [{ "code": 795302828, "code_integration": "123", "company": "ACME LTD", "phone": "135575788", "tags": [{ "tag": "companyAA" }, { "tag": "companyBB" }, { "tag": "companyCC" }, { "tag": "companyDD" } ], "status": "Y" }] const result = ttag.map(obj => obj.tags.reduce((acc, next) => [...acc, next.tag], [])).flat() console.log(result)

This should work in all browsers I believe:这应该适用于我相信的所有浏览器:

 const ttag = [ { "code": 795302828, "code_integration": "123", "company": "ACME LTD", "phone": "135575788", "tags": [{"tag": "companyAA"}, {"tag": "companyBB"}, {"tag": "companyCC"}, {"tag": "companyDD"} ], "status": "Y" }, { "code": 795302829, "code_integration": "124", "company": "ACME2 LTD", "phone": "135575789", "tags": [{"tag": "companyEE"}, {"tag": "companyFF"}, {"tag": "companyJJ"} ], "status": "Y" } ] const onlyTags = ttag.map(f => f.tags.map(t => t.tag)).reduce((p, c) => [].concat(p,c)); console.log(onlyTags);

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

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