如果对象和数组值在 javascript 中匹配,如何获取对象键
how to get the object key if the object and array value matches in javascript
我想知道如何比较对象值和数组值
在 javascript
如果array value
和object value
相同,那么return如何
object key
javascript
中的值
var result = obj.values(obj).includes(arr2) ?
'status: active' : 'status: inactive'
var obj = {
"active": 12,
"inactive": 14
"neutral": 16
}
var arr1=[12]
var arr2=[12, 14]
var arr3=[12, 16]
Expected Output
//for arr1
status: active
// for arr2
status: active
status: neutral
// for arr3
status: active
status: inactive
您应该检查数组是否包含对象值,反之亦然:
function* matches(object, array) {
for (const [key, value] of Object.entries(object)) {
if (array.includes(value))
yield key;
}
};
for (const match of matches(obj, arr1))
console.log(`status: ${match}`);
for (const match of matches(obj, arr2))
console.log(`status: ${match}`);
for (const match of matches(obj, arr3))
console.log(`status: ${match}`);
这应该有效。
您可以简单地遍历数组,然后尝试获取 Object.entries(obj)
编辑的 [key, value]
元组 return,其 value
与数组值匹配。一旦找到,你 return 元组中的 key
,即:
arr.map(v => Object.entries(obj).find(x => x[1] === v)[0]);
注意:如果你的数组可能包含对象中不的值,上面的代码将抛出错误,因为.find()
将 return 未定义。如果是这种情况,您需要捕获使用无效值的情况(防御性设计):
arr.map(v => {
const foundTuple = Object.entries(obj).find(x => x[1] === v);
return foundTuple ? foundTuple[0] : null;
});
请参阅下面的概念验证:
const obj = {
"active": 12,
"inactive": 14,
"neutral": 16
}
const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
const invalid_arr = [12, 999];
function getKeys(obj, arr) {
return arr.map(v => {
const foundTuple = Object.entries(obj).find(x => x[1] === v);
return foundTuple ? foundTuple[0] : null;
});
}
console.log(getKeys(obj, arr1));
console.log(getKeys(obj, arr2));
console.log(getKeys(obj, arr3));
console.log(getKeys(obj, invalid_arr));
另一种解决方案
const obj = { "active": 12, "inactive": 14, "neutral": 16 }
const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
const getStatuses= (obj, arr) => arr
.map(arrValue => {
const [status] = Object.entries(obj)
.find(([objKey, objValue]) => objValue === arrValue)
?? [null];
return { status };
});
console.log(getStatuses(obj, arr1));
console.log(getStatuses(obj, arr2));
console.log(getStatuses(obj, arr3));
.as-console-wrapper{min-height: 100%!important; top: 0}
使用 Object.keys()
获取键,然后通过从原始对象中获取值来过滤它们,并检查它是否包含在数组中:
const getStatus = (obj, arr) => Object.keys(obj)
.filter(key => arr.includes(obj[key]))
const obj = { "active": 12, "inactive": 14, "neutral": 16 }
const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
console.log(getStatus(obj, arr1));
console.log(getStatus(obj, arr2));
console.log(getStatus(obj, arr3));
我想知道如何比较对象值和数组值 在 javascript
如果array value
和object value
相同,那么return如何
object key
javascript
var result = obj.values(obj).includes(arr2) ?
'status: active' : 'status: inactive'
var obj = {
"active": 12,
"inactive": 14
"neutral": 16
}
var arr1=[12]
var arr2=[12, 14]
var arr3=[12, 16]
Expected Output
//for arr1
status: active
// for arr2
status: active
status: neutral
// for arr3
status: active
status: inactive
您应该检查数组是否包含对象值,反之亦然:
function* matches(object, array) {
for (const [key, value] of Object.entries(object)) {
if (array.includes(value))
yield key;
}
};
for (const match of matches(obj, arr1))
console.log(`status: ${match}`);
for (const match of matches(obj, arr2))
console.log(`status: ${match}`);
for (const match of matches(obj, arr3))
console.log(`status: ${match}`);
这应该有效。
您可以简单地遍历数组,然后尝试获取 Object.entries(obj)
编辑的 [key, value]
元组 return,其 value
与数组值匹配。一旦找到,你 return 元组中的 key
,即:
arr.map(v => Object.entries(obj).find(x => x[1] === v)[0]);
注意:如果你的数组可能包含对象中不的值,上面的代码将抛出错误,因为.find()
将 return 未定义。如果是这种情况,您需要捕获使用无效值的情况(防御性设计):
arr.map(v => {
const foundTuple = Object.entries(obj).find(x => x[1] === v);
return foundTuple ? foundTuple[0] : null;
});
请参阅下面的概念验证:
const obj = {
"active": 12,
"inactive": 14,
"neutral": 16
}
const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
const invalid_arr = [12, 999];
function getKeys(obj, arr) {
return arr.map(v => {
const foundTuple = Object.entries(obj).find(x => x[1] === v);
return foundTuple ? foundTuple[0] : null;
});
}
console.log(getKeys(obj, arr1));
console.log(getKeys(obj, arr2));
console.log(getKeys(obj, arr3));
console.log(getKeys(obj, invalid_arr));
另一种解决方案
const obj = { "active": 12, "inactive": 14, "neutral": 16 }
const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
const getStatuses= (obj, arr) => arr
.map(arrValue => {
const [status] = Object.entries(obj)
.find(([objKey, objValue]) => objValue === arrValue)
?? [null];
return { status };
});
console.log(getStatuses(obj, arr1));
console.log(getStatuses(obj, arr2));
console.log(getStatuses(obj, arr3));
.as-console-wrapper{min-height: 100%!important; top: 0}
使用 Object.keys()
获取键,然后通过从原始对象中获取值来过滤它们,并检查它是否包含在数组中:
const getStatus = (obj, arr) => Object.keys(obj)
.filter(key => arr.includes(obj[key]))
const obj = { "active": 12, "inactive": 14, "neutral": 16 }
const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
console.log(getStatus(obj, arr1));
console.log(getStatus(obj, arr2));
console.log(getStatus(obj, arr3));