javascript - 如何使用 ES6 功能减少 3 个 for 循环以提高 javascript 性能?
问题描述
我需要一个可以使用 ES6 特性减少 3 个 for 循环的解决方案。
在下面,我们有两个对象数组,其中我必须在两个位置添加一个密钥对以匹配一个。
let rootContent = [
{
'name' : 'varshan',
'textValues' : [
{ 'id' : 123 , 'value' : 'one' },
{ 'id' : 124 , 'value' : 'two' },
{ 'id' : 125 , 'value' : 'three' },
{ 'id' : 126 , 'value' : 'four' }
]
},
{
'name' : 'kathir',
'textValues' : [
{ 'id' : 223 , 'value' : 'common' },
{ 'id' : 224 , 'value' : 'maddy' },
{ 'id' : 225 , 'value' : 'winner' },
{ 'id' : 226 , 'value' : 'loser' }
]
},
{
'name' : 'karthika',
'textValues' : [
{ 'id' : 323 , 'value' : 'sticker' },
{ 'id' : 324 , 'value' : 'kammal' }
]
},
{
'name' : 'lavanya',
'textValues' : [
{ 'id' : 423 , 'value' : 'beauty' }
]
}
];
let incomingContent = [
{
'name' : 'lavanya',
'text_value' : 'beauty'
},
{
'name' : 'karthika',
'text_value' : 'kammal'
},
{
'name' : 'kathir',
'text_value' : 'maddy'
}
];
第一步是我们必须检查数组中的名称字段是否与 incomingContent
数组的名称字段匹配rootContent
,如果条件为真,那么我们必须添加一个匹配为真的键值对。
之后,我们必须使用数组的 text_value 字段找到textValues
数组 insdie 匹配对象的子数组,如果匹配,则添加一个键值对,matched 为 true,否则为 false。我在下面附上了预期的结果,我需要一个具有 ES6 功能的解决方案,例如,...。rootContent
incomingContent
map()
find()
for (let i = 0; i < incomingContent.length; i++) {
for (let j = 0; j < rootContent.length; j++) {
if (rootContent[j].name === incomingContent[i].name) {
rootContent[j]['matched'] = true;
for (let k = 0; k < rootContent[j].textValues.length; k++) {
rootContent[j].textValues[k]['matched'] = rootContent[j].textValues[k].value === incomingContent[i].text_value ? true : false;
}
}
}}
输出应该只是这样。
[
{
'name' : 'varshan',
'textValues' : [
{ 'id' : 123 , 'value' : 'one' },
{ 'id' : 124 , 'value' : 'two' },
{ 'id' : 125 , 'value' : 'three' },
{ 'id' : 126 , 'value' : 'four' }
]
},
{
'name' : 'kathir',
'matched' : true,
'textValues' : [
{ 'id' : 223 , 'value' : 'common' , 'matched' : false},
{ 'id' : 224 , 'value' : 'maddy' , 'matched' : true },
{ 'id' : 225 , 'value' : 'winner' , 'matched' : false},
{ 'id' : 226 , 'value' : 'loser' , 'matched' : false}
]
},
{
'name' : 'karthika',
'matched' : true,
'textValues' : [
{ 'id' : 323 , 'value' : 'sticker', 'matched' : false },
{ 'id' : 324 , 'value' : 'kammal' , 'matched' : true }
]
},
{
'name' : 'lavanya',
'matched' : true,
'textValues' : [
{ 'id' : 423 , 'value' : 'beauty' , 'matched' : true }
]
}
]
解决方案
将其中一个数组转换为按名称索引的对象,然后您将能够及时查找关联的名称O(1)
,而不是O(N)
时间。使用forEach
而不是for
避免手动迭代和索引,而不是x === y ? true : false
,只是使用x === y
,因为它已经解析为布尔值:
let rootContent=[{'name':'varshan','textValues':[{'id':123,'value':'one'},{'id':124,'value':'two'},{'id':125,'value':'three'},{'id':126,'value':'four'}]},{'name':'kathir','textValues':[{'id':223,'value':'common'},{'id':224,'value':'maddy'},{'id':225,'value':'winner'},{'id':226,'value':'loser'}]},{'name':'karthika','textValues':[{'id':323,'value':'sticker'},{'id':324,'value':'kammal'}]},{'name':'lavanya','textValues':[{'id':423,'value':'beauty'}]}];let incomingContent=[{'name':'lavanya','text_value':'beauty'},{'name':'karthika','text_value':'kammal'},{'name':'kathir','text_value':'maddy'}]
const rootContentByName = rootContent.reduce((a, item) => {
a[item.name] = item;
return a;
}, {});
incomingContent.forEach(({ name, text_value }) => {
const thisRoot = rootContentByName[name];
thisRoot.matched = true;
thisRoot.textValues.forEach((item) => {
item.matched = item.value === text_value;
});
});
console.log(rootContent);
虽然数组方法比循环慢一些for
,但它们通常更具可读性,并且可能更可取,除非您已经运行了性能测试并验证了特定方法占用了过多的 CPU 时间。
推荐阅读
- go - 为什么我们真的需要 Golang 中的“fallthrough”?哪个用例让 Golang 的创建者首先将其包含在内?
- google-sheets - 如何通过公式在电子表格中自动递增?
- php - 将表格中的标题(字符串)与表单输入标题(字符串)匹配
- uwp - 从应用程序资源中读取颜色
- drools - 有没有办法从 DRL 文件中的规则调用查询?
- c# - 找不到属性 [rollingStyle] 以在 [log4net.Appender.FileAppender] 上设置对象
- java - 强化 json 注入问题修复
- selenium-ide - 如何启用 Nimbus chrome 插件通过 Katalon 脚本在 Katalon Recorder 中捕获屏幕截图
- scala - Gatling 提取 xpath 值错误“尚未声明命名空间前缀‘soap’”
- javascript - 在唯一 ID 上合并两个数组