我想在对象数组中搜索字符串并返回匹配的对象。试着在这里使用es6
。
请在下面找到代码:
// set of keys
const defConfigs = [{
title: "Id",
key: "id"
},
{
title: "Tenant",
key: "tenant"
},
{
title: "Opened",
key: "opened"
},
{
title: "Title",
key: "title"
},
{
title: "Status",
key: "status"
},
{
title: "Priority",
key: "priority"
}
];
// items as array of objects
const items = [{
id: "INC000000004519",
title: "Follow-up after INC000000004507",
description: null,
urgency: "4-Low",
severity: "4-Minor/Localized"
},
{
id: "INC000000004515",
title: "network drop:↵Network Element CVU042_Johnstown get unsynchronized↵Network Element CVU043_Redman",
description: "Client network drop since 08:51 until 09:06, pleas…ork Element CVU045_North_Salem get unsynchronized",
urgency: "3-Medium",
severity: "3-Moderate/Limited"
},
{
id: "INC000000004088",
title: "not able to schedule GPEH in ABC",
description: "Contact: abc@xyz.com↵+14692669295↵…WCDMA, we are not able to schedule GPEH in ABC. I",
urgency: "4-Low",
severity: "4-Minor/Localized"
},
{
id: "INC000000004512",
title: "SR Updated - P3 - 2018-0427-0305 - xyz TELECOMMUNICATIONS ROMANIA S.R.L - Lost the mng connect",
description: null,
urgency: "4-Low",
severity: "4-Minor/Localized"
},
{
id: "INC000000004414",
title: "Acme incident 1 title",
description: "Acme incident 1 description",
urgency: "2-High",
severity: "1-Extensive/Widespread"
}
];
// trying to search for string in keys defined in defConfigs
items.filter(item =>
defConfigs.forEach((def) => {
if (def.key in item) {
return (item[def.key].toString().toLowerCase().match('low').length > 1);
}
}));
// always throws an error Uncaught TypeError: Cannot read property 'length' of null
console.log(items);

这里有3个字符串"低"并且我希望代码返回第一个项目("标题"" Fol 低 - &#34之后;);但是match
永远不会回来。
如何在对象数组中搜索字符串并返回这些对象?
答案 0 :(得分:1)
String.prototype.match()
函数将返回null
,因此您需要检查此案例。接下来,您可以使用Array.prototype.some()
函数来验证阵列中至少有一个项目是否满足您的条件。例如:
items.filter(item =>
// check if at least one key from `defConfigs` on `item` matches 'low'
defConfigs.some((def) => {
if (def.key in item) {
const matched = item[def.key].toString().toLowerCase().match('low')
// return true if item matched
return !!matched
}
// match not found by default
return false
}));
答案 1 :(得分:1)
如果你仔细观察,你会注意到:
.match
是否匹配(在不匹配时返回null
;对null.length
进行测试会产生错误)match.length > 1
...您正在使用的语法将返回一个只有一个项目的数组或null .filter
.filter
的返回值分配给任何变量以下是您需要做的事情:
var filteredItems = items.filter(function (item) {
return defConfigs.some(function (def) {
return (def.key in item)
? item[def.key].toString().toLowerCase().match('low') !== null
: false;
});
});
console.log(filteredItems);
答案 2 :(得分:0)
String.prototype.match()将返回null。如果希望匹配结果为null,则应严格将匹配结果与null进行比较。
你应该看看Array.prototype.filter。它返回一个新数组,不会修改作为参数传递的引用,原始项目是安全的(暂时)。
答案 3 :(得分:0)
单词" low"只能在紧急度属性中找到,而 defConfigs 数组不具有此字段
因此,在将其添加到阵列后,请执行以下操作:
let newItems=items.filter(item => {
let count= defConfigs.reduce((acc, def) => {
if (def.key in item) {
let arr = item[def.key].toString().toLowerCase().match(/low/gi)
return acc + (arr ? 1 : 0);
}
else {
return acc;
}
}, 0)
return count >0
});
console.log(newItems);
答案 4 :(得分:0)
在if语句if (def.key in item)
中,它将检查def.key
的值是否等于item中属性的名称。要完成您的想法,请参阅以下代码中的注释:
// set of keys
const defConfigs = [{title: "Id", key: "id"},
{title: "Tenant", key: "tenant"},
{title: "Opened", key: "opened"},
{title: "Title", key: "title"},
{title: "Status", key: "status"},
{title: "Priority", key: "priority"}];
// items as array of objects
const items = [{id: "INC000000004519", title: "Follow-up after INC000000004507", description: null, urgency: "4-Low", severity: "4-Minor/Localized"},
{id: "INC000000004515", title: "network drop:↵Network Element CVU042_Johnstown get unsynchronized↵Network Element CVU043_Redman", description: "Client network drop since 08:51 until 09:06, pleas…ork Element CVU045_North_Salem get unsynchronized", urgency: "3-Medium", severity: "3-Moderate/Limited"},
{id: "INC000000004088", title: "not able to schedule GPEH in ABC", description: "Contact: abc@xyz.com↵+14692669295↵…WCDMA, we are not able to schedule GPEH in ABC. I", urgency: "4-Low", severity: "4-Minor/Localized"},
{id: "INC000000004512", title: "SR Updated - P3 - 2018-0427-0305 - xyz TELECOMMUNICATIONS ROMANIA S.R.L - Lost the mng connect", description: null, urgency: "4-Low", severity: "4-Minor/Localized"},
{id: "INC000000004414", title: "Acme incident 1 title", description: "Acme incident 1 description", urgency: "2-High", severity: "1-Extensive/Widespread"}];
// trying to search for string in keys defined in defConfigs
items.filter(item =>
defConfigs.forEach((def) => {
//iterate through all attributes in the object
for(var key in item){
//check if the attribute exists, if it has the method 'indexOf' (if it's a string), and that it has def.key in the string
if (item[key] && item[key].indexOf && item[key].indexOf(def.key)!= -1) {
//match only accepts regular expressions which are signified in JS by enclosing the expression with forward slashes
return (item[def.key].toString().toLowerCase().match(/low/).length >1);
}
}
}));