如何在JS中以更有效的方式描述IF条件?

发布于 2025-01-31 04:15:37 字数 688 浏览 3 评论 0原文

我正在检查三个不同的变量,这些变量在下面像以下那样的字符串。

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

我每次都检查变量A,B,C是否不确定,并且是否持有某些值 我只是为结果数组中的每个变量推一个字符串。

以下是相同的代码行。

    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");
}

预期的结果应该是如果定义了var a,b,c, result 数组变量应像以下那样

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

具有更有效的描述此代码的方法。 每个变量使用条件。另外,当我使用Rhino JS引擎VER 1.7.12时,我受到某种方式受到限制。

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.

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

["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. Also , I am restricted in some ways as I am using a Rhino js engine ver 1.7.12.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

束缚m 2025-02-07 04:15:37

您可以使用初始数据创建一个对象。然后迭代它,然后附加键(例如abc),如果它与您的条件匹配。

这是一个快速演示:

// 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);

You could create an object with your initial data. Then iterate over it, and append the key (e.g. a, b or c) if it matches your condition.

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);

谈下烟灰 2025-02-07 04:15:37

我将使用对象而不是数组,以便可以指定结果元素。

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)

I'd use an object instead of an array, so that the result-elements can be specified.

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)

偏闹i 2025-02-07 04:15:37

首先,您将将3个变量存储在列表中,然后用映射循环循环

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)
    }
})

first you will store the 3 variables in a list then you loop over them with 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)
    }
})
奢欲 2025-02-07 04:15:37

我认为最有效的方法是循环的原始

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 可能会更有效,因为MAP函数创建了一个新数组,该数组填充了调用调用数组中每个元素的函数的结果。

I think the most efficient way is a raw for loop.

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.

忆伤 2025-02-07 04:15:37

我认为最好的方法是为您定义一个函数:

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);

如果要将变量名称推入结果,则:

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);

I think the best way is for you to define a 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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文