我有兩個物件需要以某種方式組合以實作所需的輸出。這是一個玩具示例。
假設我正在進行一項關于新老鼠飲食的實驗。我收集了 6 只老鼠,并給每只老鼠喂食不同的食物。幾周后,我給所有老鼠稱重并記錄每只老鼠的體重。這些權重在以下物件中給出:
const postExperimentWeights = {
mickey: 20,
minnie: 11,
jerry: 15,
stuart: 33,
gonzales: 17,
pinky: 50,
};
為了得出任何節食是否成功的結論,我有另一個資料物件,目標體重將被認為是每只老鼠的壞、平庸或好。
const targetWeights = {
mickey: {
belowIsBad: 15,
aboveIsGood: 22,
},
minnie: {
belowIsBad: 5,
aboveIsGood: 10,
},
jerry: {
belowIsBad: 16,
aboveIsGood: 20,
},
stuart: {
belowIsBad: 30,
aboveIsGood: 40,
},
gonzales: {
belowIsBad: 10,
aboveIsGood: 15,
},
pinky: {
belowIsBad: 60,
aboveIsGood: 100,
},
};
我想要的輸出是 3 個變數,每個變數都包含一個名稱陣列:
const good = ['minnie', 'gonzales']; // those were above their respective `aboveIsGood`
const mediocre = ['mickey', 'stuart']; // were between respective `belowIsBad` & `aboveIsGood`
const bad = ['jerry', 'pinky']; // below respective `belowIsBad`
是否有一種相當簡單或優雅的方式來實作所需的輸出?我通常更喜歡使用函式式方法,但無論如何都會感謝任何幫助。
uj5u.com熱心網友回復:
將它們一一回圈并將它們推入正確的陣列中。
const postExperimentWeights = {
mickey: 20,
minnie: 11,
jerry: 15,
stuart: 33,
gonzales: 17,
pinky: 50,
};
good = []
bad = []
mediocre = []
const targetWeights = {
mickey: {
belowIsBad: 15,
aboveIsGood: 22,
},
minnie: {
belowIsBad: 5,
aboveIsGood: 10,
},
jerry: {
belowIsBad: 16,
aboveIsGood: 20,
},
stuart: {
belowIsBad: 30,
aboveIsGood: 40,
},
gonzales: {
belowIsBad: 10,
aboveIsGood: 15,
},
pinky: {
belowIsBad: 60,
aboveIsGood: 100,
},
};
for (mouse in postExperimentWeights) {
let value = postExperimentWeights[mouse]
if (value < targetWeights[mouse]["belowIsBad"]) {
bad.push(mouse)
continue
}
if (value > targetWeights[mouse]["aboveIsGood"]) {
good.push(mouse)
continue
}
mediocre.push(mouse)
}
console.log(good,bad,mediocre)
uj5u.com熱心網友回復:
您可以使用陣列減少
因此,根據您的資料,您可以執行以下操作:
代碼注釋說明
// object.entries will create a paired key-value
// array of array i.e. [['mickey',20],['minnie', 11, <and so on>]]
Object.entries(postExperimentWeights).reduce(([prev, curr]) => {
// key for the property name (person name), and its value.
const [key, value] = curr;
// if it doesn't exist on the weigt, return skip? (you decide)
if (targetWeights) {
return prev;
}
if (targetWeights.belowIsBad < value) {
// store it in its respective key based on condition
return {
bad: prev.bad.concat(key);
...prev,
}
}
if (targetWeights.aboveIsGood > value) {
// store it in its respective key based on condition
return {
good: prev.bad.concat(key);
...prev,
}
}
// since this one is neither of the conditions above, its a mediocre
return {
mediocre : prev.bad.concat(key);
...prev,
}
}, {
good: [],
mediocre : [],
bad: []
});
uj5u.com熱心網友回復:
我將使用迭代過期結果Object.entries()并與目標值進行簡單比較:
const postExperimentWeights = {
mickey: 20,
minnie: 11,
jerry: 15,
stuart: 33,
gonzales: 17,
pinky: 50,
};
const targetWeights = {
mickey: {
belowIsBad: 15,
aboveIsGood: 22,
},
minnie: {
belowIsBad: 5,
aboveIsGood: 10,
},
jerry: {
belowIsBad: 16,
aboveIsGood: 20,
},
stuart: {
belowIsBad: 30,
aboveIsGood: 40,
},
gonzales: {
belowIsBad: 10,
aboveIsGood: 15,
},
pinky: {
belowIsBad: 60,
aboveIsGood: 100,
},
};
function evaluateResults(input, target) {
const good = [];
const avg = [];
const bad = [];
for(const [key,value] of Object.entries(input)) {
if(value < target[key].belowIsBad) {
bad.push(key);
}else if(value > target[key].aboveIsGood) {
good.push(key);
}else {
avg.push(key);
}
}
console.log("good:", good);
console.log("avg:", avg);
console.log("bad:", bad);
}
evaluateResults(postExperimentWeights,targetWeights);
uj5u.com熱心網友回復:
reduce使用usingpush而不是的另一種解決方案concat
const postExperimentWeights = {mickey: 20,minnie: 11,jerry: 15,stuart: 33,gonzales: 17,pinky: 50,};
const targetWeights = { mickey: {belowIsBad: 15,aboveIsGood: 22,},minnie: {belowIsBad: 5,aboveIsGood: 10,},jerry: {belowIsBad: 16,aboveIsGood: 20,},stuart: {belowIsBad: 30,aboveIsGood: 40,},gonzales: {belowIsBad: 10,aboveIsGood: 15,},pinky: {belowIsBad: 60,aboveIsGood: 100,},};
let res = Object.entries(postExperimentWeights).reduce((acc, [mouse,weight]) => {
if (weight > targetWeights[mouse].aboveIsGood) acc.good.push(mouse)
else if (targetWeights[mouse].belowIsBad < weight && weight <targetWeights[mouse].aboveIsGood) acc.mediocre.push(mouse)
else if (targetWeights[mouse].belowIsBad > weight) acc.bad.push(mouse)
return acc
}, {good: [],mediocre: [],bad: []})
console.log(res)
uj5u.com熱心網友回復:
您可以用最簡單的方式使用這段代碼
const mices = {
mickey: 20,
minnie: 11,
jerry: 15,
stuart: 33,
gonzales: 17,
pinky: 50
};
const targetWeights = {
mickey: {
belowIsBad: 15,
aboveIsGood: 22
},
minnie: {
belowIsBad: 5,
aboveIsGood: 10
},
jerry: {
belowIsBad: 16,
aboveIsGood: 20
},
stuart: {
belowIsBad: 30,
aboveIsGood: 40
},
gonzales: {
belowIsBad: 10,
aboveIsGood: 15
},
pinky: {
belowIsBad: 60,
aboveIsGood: 100
}
};
function findTheWeights(main, target) {
const good = [];
const mediocre = [];
const bad = [];
for (let mice in main) {
const { belowIsBad, aboveIsGood } = target[mice];
main[mice] > aboveIsGood
? good.push(mice)
: main[mice] < belowIsBad
? bad.push(mice)
: mediocre.push(mice);
}
return { good, bad, mediocre };
}
console.log(findTheWeights(mices, targetWeights));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/450913.html
標籤:javascript
