简体   繁体   English

使用 Array.map 并过滤对象数组,尝试删除包含 NaN 值的项目

[英]using Array.map and filter on an array of objects, trying to remove items that hold values that are NaN

As stated in title, i'm trying to remove items that hold values that are NaN in an array of objects.如标题所述,我正在尝试删除在对象数组中包含 NaN 值的项目。

distanceMatrix = [{
    'Av. Vieira Souto, 168 - Ipanema, Rio de Janeiro - RJ, 22420-004, Brazil': 'ZERO_RESULTS'
},
{
    'Rynek Główny 12, 33-332 Kraków, Poland': 1540493
},
{
    '27 Derb Lferrane, Marrakech 40000, Morocco': 2539727
},
{
    'R. Roberto Símonsen, 122 - Sé, São Paulo - SP, 01017-020, Brazil': 'ZERO_RESULTS'
}]

I am trying to run this function without avail:我试图运行此功能而无济于事:

distanceMatrix.map((location, index)=>{
    Object.values(location)
    .filter((item)=>{return !isNaN(item[0]) ? item[0] : null});
});

I am looking to reshape distanceMatrix to have items that hold only number values.我希望重塑distanceMatrix以拥有仅包含数字值的项目。 Would be happy to know what i'm doing wrong.很高兴知道我做错了什么。

An easy solution to remove objects that contain a value which is not a number would be a filter:删除包含非数字值的对象的简单解决方案是过滤器:

const filteredDistanceMatrix = distanceMatrix.filter(location => {
    const value = Object.values(location)[0]
    return !isNaN(value)
})

The filter returns a copy of distanceMatrix without the objects containing a NaN value. filter返回一个distanceMatrix的副本,其中没有包含 NaN 值的对象。

For what your sample of data shown, the objects on your array consist only of a pair of key and value .对于您显示的数据样本,数组中的objects仅包含一对keyvalue So Object.values() will return an array with only one value .所以Object.values()将返回一个只有一个value的数组。 You can check if that value is a valid number, and if not, filter the whole object out:您可以检查该值是否为有效数字,如果不是,则将整个对象过滤掉:

 const distanceMatrix = [ {'Av. Vieira Souto, 168 - Ipanema, Rio de Janeiro - RJ, 22420-004, Brazil': 'ZERO_RESULTS'}, {'Rynek Główny 12, 33-332 Kraków, Poland': 1540493}, {'27 Derb Lferrane, Marrakech 40000, Morocco': 2539727}, {'R. Roberto Símonsen, 122 - Sé, São Paulo - SP, 01017-020, Brazil': 'ZERO_RESULTS'} ]; let res = distanceMatrix.filter(o => !isNaN(+Object.values(o)[0])); console.log(res);
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;}

On the previous example, we use Unary Plus to coerces the only value of the array returned by Object.values() to a number, then we check if the result of the casting is a number or not with isNan() .在前面的示例中,我们使用Unary PlusObject.values()返回的数组的唯一值Object.values()转换为数字,然后我们使用isNan()检查转换的结果是否为数字。

这可以用于任何长度的键值对内部对象

distanceMatrix.map(location => Object.keys(location).reduce((acc, k) => { acc[k] = !isNaN(location[k]) ? location[k] : null; return acc },{}))

your current code outputs您当前的代码输出

[
    [], 
    [1540493], 
    [2539727], 
    []
]

I'm guessing you want to then reduce this array to a list of good values我猜你想然后把这个数组减少到一个好的值列表

const data = [
  { 'Av. Vieira Souto, 168 - Ipanema, Rio de Janeiro - RJ, 22420-004, Brazil': 'ZERO_RESULTS' },
  { 'Rynek Główny 12, 33-332 Kraków, Poland': 1540493 },
  { '27 Derb Lferrane, Marrakech 40000, Morocco': 2539727 },
  { 'R. Roberto Símonsen, 122 - Sé, São Paulo - SP, 01017-020, Brazil': 'ZERO_RESULTS' }
]

const out =[];
data.forEach(loc => {
    const results = Object.values(loc).filter(l => !isNaN(l));
    out.push(...results);
});
// [1540493, 2539727]

This will work:这将起作用:

 distanceMatrix = [ { 'Av. Vieira Souto, 168 - Ipanema, Rio de Janeiro - RJ, 22420-004, Brazil': 'ZERO_RESULTS' }, { 'Rynek Główny 12, 33-332 Kraków, Poland': 1540493 }, { '27 Derb Lferrane, Marrakech 40000, Morocco': 2539727 }, { 'R. Roberto Símonsen, 122 - Sé, São Paulo - SP, 01017-020, Brazil': 'ZERO_RESULTS' } ] var result = distanceMatrix.filter((item)=>{ var itemValue = parseInt(Object.values(item)[0]); return !isNaN(itemValue); }); console.log(result);

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

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