我有一個包含其他物件的物件,以及一些我不想要的數字。
我想將物件轉換為物件陣列并洗掉數字。
一個例子:
輸入
const object = {
john: {
instrument: 'violin',
age: 26
},
bob: {
instrument: 'guitar',
age: 32
},
numberIDontWant: 2,
flynn: {
instrument: 'piano',
age: 3
},
numberIDontWant2: 9
}
輸出
[
{
name: 'john',
instrument: 'violin',
age: 26
},
{
name: 'bob',
instrument: 'guitar',
age: 32
},
{
name: 'flynn',
instrument: 'piano',
age: 20
}
]
任何人都可以幫助我嗎?
uj5u.com熱心網友回復:
你可以使用Object.entriesand .map:
console.log(Object.entries({
john: {
instrument: 'violin',
age: 26
},
bob: {
instrument: 'guitar',
age: 32
},
numberIDontWant: 2,
flynn: {
instrument: 'piano',
age: 3
},
numberIDontWant2: 9
}).map(([k,v]) => typeof v === 'object' ? ({...v, name: k}) : undefined).filter(i => i))
uj5u.com熱心網友回復:
您很可能希望遍歷 ,Object.entries()以便您可以訪問鍵 ( name) 和值。目前還不是 100% 清楚包含/排除哪些值的標準是什么,但一種方法是只包含具有typeof等于的值'object'。
function objectToArray(obj) {
let arr = [];
for (let [name, value] of Object.entries(obj)) {
if (typeof value === "object") {
arr.push({
...value,
name,
});
}
}
return arr;
}
console.log(
objectToArray({
john: {
instrument: "violin",
age: 26,
},
bob: {
instrument: "guitar",
age: 32,
},
numberIDontWant: 2,
flynn: {
instrument: "piano",
age: 3,
},
numberIDontWant2: 9,
})
);
uj5u.com熱心網友回復:
你可以使用map和filter
const data = {
john: {
instrument: 'violin',
age: 26
},
bob: {
instrument: 'guitar',
age: 32
},
numberIDontWant: 2,
flynn: {
instrument: 'piano',
age: 3
},
numberIDontWant2: 9
}
const result = Object.entries(data).map(([k, v]) => {
if (typeof v === "object") return { ...v, name: k }
}).filter(x => x);
console.log(result);
uj5u.com熱心網友回復:
const array = []
for (const [key, value] of Object.entries(object)) {
if (typeof value === 'object' && value != null) {
array.push({
name: key,
instrument: value.instrument,
age: value.age
})
}
}
uj5u.com熱心網友回復:
我會這樣做:
const object = {
john: {
instrument: 'violin',
age: 26
},
bob: {
instrument: 'guitar',
age: 32
},
numberIDontWant: 2,
flynn: {
instrument: 'piano',
age: 3
},
numberIDontWant2: 9
}
const result = [];
for (const i of Object.entries(object)) {
if (typeof i[1] == 'object') {
const obj = { name: i[0], ...i[1] }
result.push(obj)
}
}
console.log(result);
uj5u.com熱心網友回復:
const object = {
john: {
instrument: "violin",
age: 26,
},
bob: {
instrument: "guitar",
age: 32,
},
numberIDontWant: 2,
flynn: {
instrument: "piano",
age: 3,
},
numberIDontWant2: 9,
};
const arr = [];
for (let [key, value] of Object.entries(object)) {
if (
typeof value === "object" &&
value.hasOwnProperty("instrument") &&
value.hasOwnProperty("age")
)
arr.push({
name: key,
...value,
});
}
console.log(arr);
uj5u.com熱心網友回復:
- 使用Object.entries從您的物件創建一個條目(鍵/值對)陣列
- 使用Array.prototype.reduce將一個陣列縮減為另一個陣列
- 使用typeof運算子檢查型別是一個物件,同時減少
const object = {
john: {instrument: 'violin', age: 26},
bob: {instrument: 'guitar', age: 32},
numberIDontWant: 2,
flynn: {instrument: 'piano', age: 3},
numberIDontWant2: 9
};
const result = Object.entries(object).reduce((arr, [k, v]) =>
(typeof v === "object" && arr.push({...v, name:k}), arr)
, []);
console.log(result);
uj5u.com熱心網友回復:
flatMap可用于過濾和映射一次,節省呼叫的需要reduce。
我會這樣使用它:
const convert = (o) =>
Object.entries(o).flatMap(([name, val]) => typeof val == 'object' ? [{name, ...val}] : [])
const object = {john: {instrument: 'violin', age: 26}, bob: {instrument: 'guitar', age: 32}, numberIDontWant: 2, flynn: {instrument: 'piano', age: 3}, numberIDontWant2: 9}
console.log(convert(object))
.as-console-wrapper {max-height: 100% !important; top: 0}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477473.html
標籤:javascript 数组 目的
上一篇:覆寫物件陣列中的物件
