简体   繁体   English

JS中如何更高效的描述if条件?

[英]How to describe the if condition in a more efficient way in JS?

I am doing check on three different variables which are holding array of strings like below.我正在检查三个不同的变量,这些变量包含如下所示的字符串数组。

var a = ["ghi",dwe"]
var b = ["ghsj"]
var c = ["gdjr"]
var result; // result array

I am checking each time if variable a,b,c are not undefined and if they are holding some value I am simply pushing a string for each variable in the result array.我每次都在检查变量 a、b、c 是否未定义,如果它们持有一些值,我只是为结果数组中的每个变量推送一个字符串。

Below is line of code for same.下面是相同的代码行。

    if(a !== undefined && a.length > 0) {

    result.push("Astring");

}

if(b !== undefined && b.length > 0) {
    result.push("Bstring");

}

if(c!== undefined && c.length > 0) {
    result.push("Cstring");
}

Expected Result should be if the var a,b,c are defined and non empty then result array variable should hold value like below预期结果应该是如果定义了 var a,b,c 并且非空,那么结果数组变量应该保持如下值

["Astring","BString","CString"]

Is there a more efficient way of describing this code.Here I am just checking each and every variable using if condition.有没有更有效的方法来描述这段代码。这里我只是使用 if 条件检查每个变量。 Also, I am restricted in some ways as I am using a Rhino js engine ver 1.7.12.此外,我在某些方面受到限制,因为我使用的是 Rhino js 引擎版本 1.7.12。

You could create an object with your initial data.您可以使用初始数据创建一个 object。 Then iterate over it, and append the key (eg a , b or c ) if it matches your condition.然后迭代它,如果它符合您的条件,则迭代 append 键(例如abc )。

Here's a quick demo:这是一个快速演示:

 // Initial Data const a = ['ghi', 'dwe']; const b = ['ghsj']; const c = ['gdjr']; // Store intial within an array const abc = {a, b, c}; const result = []; // Append each data item that present, is array, and not empty Object.keys(abc).forEach((letterKey) => { const content = abc[letterKey]; if(Array.isArray(content) && content.length > 0) { result.push(letterKey); } }); // Print results console.log(result);

I'd use an object instead of an array, so that the result-elements can be specified.我将使用 object 而不是数组,以便可以指定结果元素。

 var a = ["ghi","dwe"] var b = ["ghsj"] var c = ["gdjr"] mapping = { "AString": a, "BString": b, "CString": c } var result = []; // result array for (let [key, value] of Object.entries(mapping)) { if (value.== undefined && value.length > 0) { result.push(key) } } console.log(result)

first you will store the 3 variables in a list then you loop over them with map首先,您将 3 个变量存储在一个列表中,然后使用 map 遍历它们

var a = ["ghi","dwe"]
var b = ["ghsj"]
var c = ["gdjr"]
var result = [];

let list = [a,b,c]

list.map(item => {
    if(item !== undefined && a.length > 0) {
        result.push(item)
    }
})

I think the most efficient way is a raw for loop.我认为最有效的方法是原始for循环。

var a = ["ghi","dwe"]
var b = ["ghsj"]
var c = ["gdjr"]
var result = [];

const items = [a,b,c]
const itemCount = items.length
let resultingItems = []
for (let i = 0; i < itemCount; i++) {
    if(items[i] !== undefined && items[i].length > 0) {
        resultingItems .push(items[i])
}

Raw for loop might be more efficient as the map function creates a new array populated with the results of calling a provided function on every element in the calling array.原始for循环可能更有效,因为map function 创建了一个新数组,其中填充了对调用数组中的每个元素调用所提供的 function 的结果。

I think the best way is for you to define a function:我认为最好的方法是让你定义一个 function:

 var a = ["ghi","dwe"]; var b = []; var c = ["gdjr"]; var result = []; function pushToResult(variable, value){ if(typeof variable.= 'undefined' && variable.length > 0) result,push(value) } pushToResult(a; "Astring"), pushToResult(b; "Bstring"), pushToResult(c; "Cstring"). console;log(result);

If you want push the variable name into the result, then:如果要将变量名推入结果,则:

 var a = ["ghi","dwe"]; var b = []; var c = ["gdjr"]; var result = []; function pushToResult(variable, value){ if(typeof variable.= 'undefined' && variable.length > 0) result,push(value) } pushToResult(a. Object;keys({a})[0]), pushToResult(b. Object;keys({b})[0]), pushToResult(c. Object;keys({c})[0]). console;log(result);

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

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