简体   繁体   English

如何遍历 object 数组以查找包含相同单词的键值-Javascript

[英]How to loop through an array of object to finding key values that includes same words-Javascript

I have an array of object with textnames我有一个带有文本名称的 object 数组

array= [{
  text:"ABC",
  someobj:{}
  length: 3
 },{
  text:"XYZ",
  someobj:{},
  length: 4
 },{
  text:"TES",
  someobj:{},
  length: 2
 },{
  text:"ABC_VALUE",
  someobj:{},length: 3
 },{
  text:"TES_VALUE",
  someobj:{},length: 10
 },{
  text:"SME",
  someobj:{},length: 39
 },{
  text:"SME_VALUE",
  someobj:{}, length: 1
 }
]

As you can see I have _VALUE present in some text names.如您所见,我在某些文本名称中存在_VALUE I'm trying to get the total length of all the matching values, ie length of "ABC" + length of "ABC_VALUE"(6), similarly length of "SME_VALUE" + length of "SME" (40).我正在尝试获取所有匹配值的总长度,即“ABC”的长度+“ABC_VALUE”的长度(6),类似的“SME_VALUE”的长度+“SME”的长度(40)。

I tried with switch case and if condition.我尝试使用 switch case 和 if 条件。

function getVlaues(obj)
let length=0;
    else if(obj.text.includes("SME")) {length += obj.length;}
    else if (obj.text.includes("ABC")) {length += obj.length;}
    else if (obj.text.includes("TES")) {length += obj.length;}

    return length;
}

but this does not return me the total length of the 2 object.但这并没有返回 2 object 的总长度。 Any idea how to do this?知道怎么做吗?

You can split text on _ and use the first value as key and calculate length for each key您可以在_上拆分文本并将第一个值用作键并计算每个键的长度

 let array = [{text: "ABC",someobj: {},length: 3}, {text: "XYZ",someobj: {},length: 4}, {text: "TES",someobj: {},length: 2}, {text: "ABC_VALUE",someobj: {},length: 3}, {text: "TES_VALUE",someobj: {},length: 10}, {text: "SME",someobj: {},length: 39}, {text: "SME_VALUE",someobj: {},length: 1}] let final = array.reduce((op, inp) => { let text = inp.text.split('_', 1)[0] op[text] = op[text] || 0 op[text] += inp.length return op }, {}) console.log(final)

Or if you're sure the text only differs by _value in the end then you can replace that part and use as key或者,如果您确定text最后仅由_value不同,那么您可以替换该部分并用作键

 let array = [{text: "ABC",someobj: {},length: 3}, {text: "XYZ",someobj: {},length: 4}, {text: "TES",someobj: {},length: 2}, {text: "ABC_VALUE",someobj: {},length: 3}, {text: "TES_VALUE",someobj: {},length: 10}, {text: "SME",someobj: {},length: 39}, {text: "SME_VALUE",someobj: {},length: 1}] let final = array.reduce((op, inp) => { let text = inp.text.replace(/_value$/gi,'') op[text] = op[text] || 0 op[text] += inp.length return op }, {}) console.log(final)

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

相关问题 JavaScript嵌套对象数组,如何遍历数组的“ KEY”? - JavaScript nested object array, How to loop through the 'KEY' of array? 如何循环通过 Javascript object 检查键和值? - How do I loop through Javascript object checking key and values? 如何检查数组是否包含Javascript中具有相同字段的对象? - How do I check if an array includes an object with the same field in Javascript? 如何循环并返回javascript对象的值? - How to loop through and return the values of a javascript object? 使用javascript查找同一对象数组中存在的两个值 - Finding two values existing in same object array using javascript Javascript 循环通过嵌套的 object 并尝试查看所有值是否相同 - Javascript loop through a nested object and trying to see if all values are same or not 如何遍历对象键值和嵌套值 - How to loop through object key values and nested values 遍历嵌套的json数组对象并求和JavaScript - Loop through nested json array object and sum values javascript 如何循环获取数组Javascript中的对象的值? - How to loop to get values of object in array Javascript? 如何遍历对象数组并根据 JavaScript 中的条件添加新的对象键? - How to loop through array of objects and add new object key based on condition in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM