所以我試圖遍歷一個物件,看看它是否有一個特定的“id”鍵。我試過hasOwnProperty().includes 和“如果專案中的‘id’”。我也只是嘗試檢查物件的實際長度,但仍然無法正常作業。我不知道為什么這些選項都不起作用。
在這張圖片中,我嘗試計算flightsbyId物件中的鍵數。它所做的只是回傳專案中的字符數而不是父物件中的物件數。

看控制臺。它對所有物件都說 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
那有意義嗎?
uj5u.com熱心網友回復:
這是查找特定 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")
}
}
uj5u.com熱心網友回復:
every和some有選擇的操作資料項的串列時,得到一個布爾結果/答案的方法。
另外應該提到的是,這兩種方法都會提前退出陣列的迭代,這意味著只要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 想要find一個flight專案,id那么上面提供的代碼只需要通過它的方法名稱進行更改。
因此,someand之間的唯一區別find是前者的回傳值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; }
uj5u.com熱心網友回復:
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;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/325834.html
標籤:javascript 数组 找 条件语句
