JS - 检查数组中是否存在一个值,如果为真则赋值
JS - Check for existence of a value in array and if true assign a value
我正在检查数组中是否存在某个值,如果存在,我想分配一个特定于 myArray 中的值的值。下面的代码有效,但有很多重复。有没有更好的做事方式?
const myArray = ['a', 'b', 'c', 'd'];
const aExists = myArray.indexOf('a') > -1;
const bExists = myArray.indexOf('b') > -1;
const cExists = myArray.indexOf('c') > -1;
return [
(aExists ? 'value-for-a' : undefined),
(bExists ? 'or-something-for-b' : undefined),
(cExists ? 'different-value-for-c' : undefined)
].filter(x => x !== undefined).toString();
创建 keys/values 的字典 (dict
)。使用 Object.keys()
, and iterate the keys with Array.reduce()
. Find the existence of the keys in myArray
using Array.includes()
,并将 dict
中的匹配键添加到结果中:
const dict = {
'a': 'value-for-a',
'b': 'or-something-for-b',
'c': 'different-value-for-c'
};
const myArray = ['a', 'b', 'c', 'd'];
const result = Object.keys(dict)
.reduce((r, k) => {
if(myArray.includes(k)) r.push(dict[k]);
return r;
}, []);
console.log(result);
显而易见的是使用一系列 if
s
const myArray = ['a', 'b', 'c', 'd'];
if (myArray.indexOf('a') > -1) {
return 'value-for-a';
}
if (myArray.indexOf('b') > -1) {
return 'or-something-for-b';
}
if (myArray.indexOf('c') > -1) {
return 'different-value-for-c';
}
return '';
或值映射和循环
const myArray = ['a', 'b', 'c', 'd'];
const values = {a: 'value-for-a', b: 'or-something-for-b', c: 'different-value-for-c'};
for (const key of Object.keys(values)) {
if (myArray.indexOf(key)) {
return values[key];
}
}
return '';
您可以创建一个对象数据结构,您要检查其在数组中的索引并获取新值。当该键的索引存在于数组 myArray
中时,您只需将与该索引对应的值推送到结果数组中:
const mapObj = {
'a': 'value-for-a',
'b': 'or-something-for-b',
'c': 'different-value-for-c'
};
const myArray = ['a', 'b', 'c', 'd'];
var res = [];
Object.keys(mapObj).forEach((key)=>{
if(myArray.indexOf(key) !== -1){
res.push(mapObj[key]);
}
});
console.log(res);
Also, I would suggest to use indexOf()
instead of includes()
as includes()
will give you error in IE browser.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Browser_compatibility
为所需的新数组的可能值创建一个对象,然后遍历其条目以将其转换为所需的格式:
const myArray = ['a', 'b', 'c', 'd'];
const newArrValues = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c',
};
const result = Object.entries(newArrValues)
.filter(([key]) => myArray.includes(key))
.map(([_, value]) => value)
.toString();
console.log(result);
您只需要为所有预先指定的值创建一个映射器,并通过检查其键在映射器中的存在来过滤 myArray
。然后使用 map
创建一个包含预期值的新数组。
let myArray = ['a', 'b', 'c', 'd'];
let values = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c'
};
let results = myArray.filter(v => values[v]).map(v => values[v]);
console.log(results);
如果您想要更快的解决方案,则可以改用它。
let myArray = ['a', 'b', 'c', 'd'];
let values = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c'
};
let results =[];
for(let v of myArray) {
if(!values[v]) continue;
results.push(values[v]);
}
console.log(results);
您可以使用 in
operator 并使用所需字符串的对象过滤数组的项目,然后映射值。
var array = ['a', 'b', 'd'];
data = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c',
e: 'foo'
},
result = array
.filter(k => k in data)
.map(k => data[k]);
console.log(result);
我正在检查数组中是否存在某个值,如果存在,我想分配一个特定于 myArray 中的值的值。下面的代码有效,但有很多重复。有没有更好的做事方式?
const myArray = ['a', 'b', 'c', 'd'];
const aExists = myArray.indexOf('a') > -1;
const bExists = myArray.indexOf('b') > -1;
const cExists = myArray.indexOf('c') > -1;
return [
(aExists ? 'value-for-a' : undefined),
(bExists ? 'or-something-for-b' : undefined),
(cExists ? 'different-value-for-c' : undefined)
].filter(x => x !== undefined).toString();
创建 keys/values 的字典 (dict
)。使用 Object.keys()
, and iterate the keys with Array.reduce()
. Find the existence of the keys in myArray
using Array.includes()
,并将 dict
中的匹配键添加到结果中:
const dict = {
'a': 'value-for-a',
'b': 'or-something-for-b',
'c': 'different-value-for-c'
};
const myArray = ['a', 'b', 'c', 'd'];
const result = Object.keys(dict)
.reduce((r, k) => {
if(myArray.includes(k)) r.push(dict[k]);
return r;
}, []);
console.log(result);
显而易见的是使用一系列 if
s
const myArray = ['a', 'b', 'c', 'd'];
if (myArray.indexOf('a') > -1) {
return 'value-for-a';
}
if (myArray.indexOf('b') > -1) {
return 'or-something-for-b';
}
if (myArray.indexOf('c') > -1) {
return 'different-value-for-c';
}
return '';
或值映射和循环
const myArray = ['a', 'b', 'c', 'd'];
const values = {a: 'value-for-a', b: 'or-something-for-b', c: 'different-value-for-c'};
for (const key of Object.keys(values)) {
if (myArray.indexOf(key)) {
return values[key];
}
}
return '';
您可以创建一个对象数据结构,您要检查其在数组中的索引并获取新值。当该键的索引存在于数组 myArray
中时,您只需将与该索引对应的值推送到结果数组中:
const mapObj = {
'a': 'value-for-a',
'b': 'or-something-for-b',
'c': 'different-value-for-c'
};
const myArray = ['a', 'b', 'c', 'd'];
var res = [];
Object.keys(mapObj).forEach((key)=>{
if(myArray.indexOf(key) !== -1){
res.push(mapObj[key]);
}
});
console.log(res);
Also, I would suggest to use
indexOf()
instead ofincludes()
asincludes()
will give you error in IE browser. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes#Browser_compatibility
为所需的新数组的可能值创建一个对象,然后遍历其条目以将其转换为所需的格式:
const myArray = ['a', 'b', 'c', 'd'];
const newArrValues = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c',
};
const result = Object.entries(newArrValues)
.filter(([key]) => myArray.includes(key))
.map(([_, value]) => value)
.toString();
console.log(result);
您只需要为所有预先指定的值创建一个映射器,并通过检查其键在映射器中的存在来过滤 myArray
。然后使用 map
创建一个包含预期值的新数组。
let myArray = ['a', 'b', 'c', 'd'];
let values = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c'
};
let results = myArray.filter(v => values[v]).map(v => values[v]);
console.log(results);
如果您想要更快的解决方案,则可以改用它。
let myArray = ['a', 'b', 'c', 'd'];
let values = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c'
};
let results =[];
for(let v of myArray) {
if(!values[v]) continue;
results.push(values[v]);
}
console.log(results);
您可以使用 in
operator 并使用所需字符串的对象过滤数组的项目,然后映射值。
var array = ['a', 'b', 'd'];
data = {
a: 'value-for-a',
b: 'or-something-for-b',
c: 'different-value-for-c',
e: 'foo'
},
result = array
.filter(k => k in data)
.map(k => data[k]);
console.log(result);