简体   繁体   English

在array.forEach函数语句中使用if语句在对象数组中选择子对象

[英]Using if statement in array.forEach function statement to choose sub-object in array of objects

I cannot get an if statement to work inside a function called by a forEach array loop. 我无法通过if语句在forEach数组循环调用的函数中工作。

I have an array with objects, (with an object) 我有一个带有对象的数组,(带有一个对象)

arrofobj = [   
{"thing_id":"1a", "val": 1, "Type": "Switch","ValType":{"0":"Open","1":"Closed"}},  
{"thing_id":"1b", "val": 72, "Type": "Sensor","ValType":{"0":"%"}}]

I would like to test if the Type is a switch, in order to write info in a new field of the objects of the array CatX : 我想测试Type是否是一个开关,以便在数组CatX的对象的新字段中写入信息:
- when it is, I want to use the val value to determine which ValType element to use in a new variable of array arrofobj . -是的时候,我想使用val值来确定要在数组arrofobj的新变量中使用哪个ValType元素。
- if not, I want to use the arrofobj.ValType.0 value -如果没有,我想使用arrofobj.ValType.0值

const getCat = function(){
    if(arrofobj.Type !== 'Switch') 
        arrofobj.ValType'.0' 
    } else {
        arrofobj.ValType.(arrofobj.val)
};

arrofobj.forEach(p => p.CatX = getCat() ); 

I am not getting the lint to accept the code, so cannot test. 我没有让棉绒接受代码,所以无法测试。

1) You have to use bracket notation to access properties as strings. 1)您必须使用方括号符号来访问属性作为字符串。

2) You have to close the brackets on if/else correctly. 2)您必须正确关闭if / else括号。

3) You have to return something from inside getCat to have something to assign to p.CatX 3)您必须从getCat内部返回某些内容才能将某些内容分配给p.CatX

4) You have to actually send the object to getCat inside the loop. 4)您必须在循环内实际将对象发送到getCat。

 const arrofobj = [ {"thing_id":"1a", "val": 1, "Type": "Switch","ValType":{"0":"Open","1":"Closed"}}, {"thing_id":"1b", "val": 72, "Type": "Sensor","ValType":{"0":"%"}} ]; const getCat = function( obj ){ if(obj.Type !== 'Switch') { return obj.ValType[ '0' ] } else { return obj.ValType[ obj.val ]; } }; arrofobj.forEach(p => { p.CatX = getCat(p); }); console.log( arrofobj ); 

Just to add to Shilly's answer: 只是为了补充Shilly的答案:

1) In the long-run, if this is data you're creating yourself and not something from a 3rd-party endpoint, you'll find standardising the format of your object property key names (in camelCase) to be easier to work with. 1)从长远来看,如果这是您在创建自己的数据,而不是第三方端点提供的数据,您会发现标准化对象属性键名的格式(在camelCase中)更加容易使用。 It won't introduce as many bugs to your code if they're identically formatted. 如果它们的格式相同,它将不会在您的代码中引入那么多错误。

2) You can use object destructuring assignment and a ternary operator to shorten the code footprint a little. 2)您可以使用对象分解分配三元运算符来稍微缩短代码占用空间。

 const arrofobj = [ { id: '1a', val: 1, type: 'Switch', valType: { '0': 'Open', '1': 'Closed' } }, { id: '1b', val: 72, type: 'Sensor', valType: { '0': '%' } } ]; function getCat(obj) { // Deconstruct the properties from obj const { type, valType, val } = obj; // Use a ternary operator to decide what to return return type === 'Switch' ? valType[val] : valType['0']; } arrofobj.forEach(obj => { obj.catX = getCat(obj); }); console.log(arrofobj); 

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

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