json 如果嵌套对象中有required属性,则根据条件过滤对象数组

zbq4xfa0  于 11个月前  发布在  其他
关注(0)|答案(4)|浏览(214)

我正在试着解一个很难的计算,但是我哪儿也解不出来。有人能帮帮我吗?
这里有一个简单的例子。

const filterCityBy = "NY";
const data = [ 
  { name: "Harry, age: 45, city: "NY" },
  { name: "Mike, age: 36, city: "CA" }
]

const filteredData = data.filter(x = x.city === filterCityBy);

output = [ { name: "Harry, age: 45 } ]

字符串
这是自我解释。我试图过滤data与值filterCityBy和获得所需的output
但我的要求如下

const filterCityBy = "NY";
const data = [ 
  { name: "Harry, age: 45, totalCities: [{ city: "NY"}]},
  { name: "Mike, age: 36, totalCities: [{ city: "NY"}, {city: "CA"} }] }
]

output = [ { name: "Harry, age: 45 }, { name: "Mike, age: 36 } ]


我如何才能实现所需的输出。Harry和Mike在他们的totalCities数组中都有城市作为NY。一旦城市出现在他们的totalCities数组中,我就只需要他们的nameage
有人可以帮助如何得到这个。

thigvfpy

thigvfpy1#

您可以过滤数组并Map结果而不使用totalCities

const
    filterCityBy = "NY";
    data = [{ name: "Harry", age: 45, totalCities: [{ city: "NY" }] }, { name: "Mike", age: 36, totalCities: [{ city: "NY" }, { city: "CA" }] }],
    result = data
        .filter(({ totalCities }) => totalCities.some(({ city }) => city === filterCityBy))
        .map(({ totalCities, ...o}) => o);

console.log(result);

个字符

nkhmeac6

nkhmeac62#

将合并filter()some()结合,检查totalCities中的 some 是否与filterCityBy匹配。
然后使用map()只得到nameage键,通过使用解构赋值(...r)删除totalCities

const filterCityBy = "NY";
const data = [ 
  { name: "Harry", age: 45, totalCities: [{ city: "NY"}]},
  { name: "Mike", age: 36, totalCities: [{ city: "NY"}, {city: "CA"} ] }
]

const output = data
    .filter(p => p?.totalCities.some(({ city }) => city === filterCityBy))
    .map(({ totalCities, ...r }) => r);
                   
console.log(output);

字符串

[
  {
    "name": "Harry",
    "age": 45
  },
  {
    "name": "Mike",
    "age": 36
  }
]

u91tlkcl

u91tlkcl3#

检查你的示例代码和结果。一旦你修复了代码,filter方法本身不会给予那个结果。你还需要map方法,就像其他人已经指出的那样。这里是另一种方法:

const 
    filterCityBy = "NY",
    data = [ 
      { name: "Harry", age: 45, totalCities: [{ city: "NY"}]},
      { name: "Mike", age: 36, totalCities: [{ city: "NY"}, {city: "CA"}] }
    ],
    
    filteredData = data.filter(
        ({totalCities}) => totalCities.some(({city}) => city === filterCityBy)
    )
    .map(({name,age}) => ({name,age}));
    
console.log( filteredData );

字符串

mklgxw1f

mklgxw1f4#

我会避免Map到没有totalCities的对象。只是不要在过滤数组中使用totalCities。此外,Map是否有助于内存优化也是值得怀疑的。此外,解构可能意味着更冗长和复杂的语法。

` Chrome/119
----------------------------------------------------------
Alexander     1.00x  |  x10000000  181  188  190  202  216
Nina Scholz   1.70x  |  x10000000  307  311  313  314  319
----------------------------------------------------------
https://github.com/silentmantra/benchmark `

字符串

const
    filterCityBy = "NY";
    data = [{ name: "Harry", age: 45, totalCities: [{ city: "NY" }] }, { name: "Mike", age: 36, totalCities: [{ city: "NY" }, { city: "CA" }] }];
    
// @benchmark Nina Scholz

filteredData = data.filter(
        ({totalCities}) => totalCities.some(({city}) => city === filterCityBy)
    )
    .map(({name,age}) => ({name,age}));
    
// @benchmark Alexander
data.filter(p => p.totalCities.some(e => e.city === filterCityBy))

/*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));
.as-console-wrapper { max-height: 100% !important; top: 0; }

的数据

相关问题