如何遍历对象的 属性 值以验证某些值(以及对象)彼此特定的 属性 值?
How does one iterate through an object's property values in order to verify for some values (objects as well) each another specific property value?
所以我试图遍历一个对象以查看它是否具有特定的“id”键。我试过 hasOwnProperty()
、.includes 和“if 'id' in item”。我也刚刚尝试检查对象的实际长度,但仍然无法正常工作。我不知道为什么 none 这些选项有效。
在这张图片中,我试图计算 flightsbyId
对象中的键数。它所做的只是 return 项目中的字符数,而不是父对象中的对象数。
看看控制台。它对所有对象都说 false,但是除了一个对象之外,所有对象都有一个 id 键。
前对象:
const formik = {
randomObj: "something",
values: {
flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
},
attributes: null,
};
我期望写成伪代码的内容:
if 'id' in flight1, console.log(true), else console.log(false)
或
if length(flight1)=== a number, return true
这有意义吗?
const idsOrLength = Object.keys(formik.values.flightsById).map(({ itemKey }) => {
const item = formik.values.flightsById[itemKey];
const { id } = item;
const length = Object.keys(item).length
return id || length;
});
这是查找特定 ID 的一种方法。我安慰找到的物体和那个特定物体的长度。但是我不太清楚你到底想要什么。
let formik = {
"randomObj": 'something',
"values": {
flight1: {
'name': "flight1",
'timecreated': "sometime",
id: 548497984
},
flight2: {
'name': "flight2",
'timecreated': "sometime",
id: 548497982
},
flight3: {
'name': "flight3",
'timecreated': "sometime",
id: 548497989
},
flight4: {
'name': "flight4",
'timecreated': "sometime",
id: 548497981
}
},
attributes: 'null'
}
for (let key in formik.values) {
if (formik.values[key].id === 548497984) {
console.log(formik.values[key])
console.log("Length is: ", Object.keys(formik.values[key]).length)
break;
} else {
console.log("no matches")
}
}
every
and some
是操作数据项列表时获取布尔值result/answer的选择方法
另外应该提到的是,这两种方法都会提前退出数组的迭代,这意味着一旦 every
条件失败或 some
条件匹配。
const formik = {
randomObj: "something",
values: {
flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
},
attributes: null,
};
console.log(
'... does flight with `id: 548497989` exist ?..',
Object
.values(formik.values)
.some(({ id }) => id === 548497989)
)
console.log(
'... does flight with `id: 123456789` exist ?..',
Object
.values(formik.values)
.some(({ id }) => id === 123456789)
)
.as-console-wrapper { min-height: 100%!important; top: 0; }
并且如果 OP 想要通过 id
find
一个 flight
项目,那么上面提供的代码只需要通过其方法名称进行更改。
因此 some
和 find
之间的唯一区别是前者的 return 值 true
/false
和 undefined
值或后者的匹配数组项。
const formik = {
randomObj: "something",
values: {
flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
},
attributes: null,
};
console.log(
'... find flight with `id: 548497989` ...',
Object
.values(formik.values)
.find(({ id }) => id === 548497989)
);
console.log(
'... find flight with `id: 123456789` ...',
Object
.values(formik.values)
.find(({ id }) => id === 123456789)
);
console.log(
'... how many properties has flight with `id: 548497989` ?..',
Object.keys(
Object
.values(formik.values)
.find(({ id }) => id === 548497989) ?? {}
).length || null // || -1 // || 'none'
);
console.log(
'... how many properties has flight with `id: 123456789` ?..',
Object.keys(
Object
.values(formik.values)
.find(({ id }) => id === 123456789) ?? {}
).length || null // || -1 // || 'none'
);
.as-console-wrapper { min-height: 100%!important; top: 0; }
所以我试图遍历一个对象以查看它是否具有特定的“id”键。我试过 hasOwnProperty()
、.includes 和“if 'id' in item”。我也刚刚尝试检查对象的实际长度,但仍然无法正常工作。我不知道为什么 none 这些选项有效。
在这张图片中,我试图计算 flightsbyId
对象中的键数。它所做的只是 return 项目中的字符数,而不是父对象中的对象数。
看看控制台。它对所有对象都说 false,但是除了一个对象之外,所有对象都有一个 id 键。
前对象:
const formik = {
randomObj: "something",
values: {
flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
},
attributes: null,
};
我期望写成伪代码的内容:
if 'id' in flight1, console.log(true), else console.log(false)
或
if length(flight1)=== a number, return true
这有意义吗?
const idsOrLength = Object.keys(formik.values.flightsById).map(({ itemKey }) => {
const item = formik.values.flightsById[itemKey];
const { id } = item;
const length = Object.keys(item).length
return id || length;
});
这是查找特定 ID 的一种方法。我安慰找到的物体和那个特定物体的长度。但是我不太清楚你到底想要什么。
let formik = {
"randomObj": 'something',
"values": {
flight1: {
'name': "flight1",
'timecreated': "sometime",
id: 548497984
},
flight2: {
'name': "flight2",
'timecreated': "sometime",
id: 548497982
},
flight3: {
'name': "flight3",
'timecreated': "sometime",
id: 548497989
},
flight4: {
'name': "flight4",
'timecreated': "sometime",
id: 548497981
}
},
attributes: 'null'
}
for (let key in formik.values) {
if (formik.values[key].id === 548497984) {
console.log(formik.values[key])
console.log("Length is: ", Object.keys(formik.values[key]).length)
break;
} else {
console.log("no matches")
}
}
every
and some
是操作数据项列表时获取布尔值result/answer的选择方法
另外应该提到的是,这两种方法都会提前退出数组的迭代,这意味着一旦 every
条件失败或 some
条件匹配。
const formik = {
randomObj: "something",
values: {
flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
},
attributes: null,
};
console.log(
'... does flight with `id: 548497989` exist ?..',
Object
.values(formik.values)
.some(({ id }) => id === 548497989)
)
console.log(
'... does flight with `id: 123456789` exist ?..',
Object
.values(formik.values)
.some(({ id }) => id === 123456789)
)
.as-console-wrapper { min-height: 100%!important; top: 0; }
并且如果 OP 想要通过 id
find
一个 flight
项目,那么上面提供的代码只需要通过其方法名称进行更改。
因此 some
和 find
之间的唯一区别是前者的 return 值 true
/false
和 undefined
值或后者的匹配数组项。
const formik = {
randomObj: "something",
values: {
flight1: { name: "flight1", timecreated: "sometime", id: 548497984 },
flight2: { name: "flight2", timecreated: "sometime", id: 548497982 },
flight3: { name: "flight3", timecreated: "sometime", id: 548497989 },
flight4: { name: "flight4", timecreated: "sometime", id: 548497981 },
},
attributes: null,
};
console.log(
'... find flight with `id: 548497989` ...',
Object
.values(formik.values)
.find(({ id }) => id === 548497989)
);
console.log(
'... find flight with `id: 123456789` ...',
Object
.values(formik.values)
.find(({ id }) => id === 123456789)
);
console.log(
'... how many properties has flight with `id: 548497989` ?..',
Object.keys(
Object
.values(formik.values)
.find(({ id }) => id === 548497989) ?? {}
).length || null // || -1 // || 'none'
);
console.log(
'... how many properties has flight with `id: 123456789` ?..',
Object.keys(
Object
.values(formik.values)
.find(({ id }) => id === 123456789) ?? {}
).length || null // || -1 // || 'none'
);
.as-console-wrapper { min-height: 100%!important; top: 0; }