我有陣列:
const values = [
{
id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
capacity: 8。
},
{
id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
capacity: 6。
},
{
id: "djkfu4-xvvxvx-4344-sddsd"/span>,
capacity: 1.
}
]
const allowedValues = [
{
id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
capacity: 3。
},
{
id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
capacity: 2.
}
我需要檢查來自values的確切物件的容量是否超過allowedValues的容量,并回傳一個錯誤。
因此,基于這個例子,預期的回應是:
[
{ 'c22d8560-2d42-45c0-9ca0-91a4eabd193d': '允許的容量是3' },
{ 'be63bd14-f448-4dc4-8087-3b895095d0bc': '允許的容量是2' },
];
有可能實作嗎?
uj5u.com熱心網友回復:
Code
const response = values
.map((value) =>/span> {
const allowedValue = allowedValues.find((allowedValue) => {
return allowedValue.id ==value.id。
}) ?? { capacity: Infinity };
return {
...值。
allowedCapacity: allowedValue.capacity。
};
})
.filter((value) =>/span> {
return value.capacity > value.allowedCapacity;
})
.map((value) => {
return {
[value.id]。`允許的容量是${value.allowedCapacity}`。
};
});
解釋
將值映射到一個物件串列,該串列看起來像{id, capacity, allowedCapacity},并使用Infinity表示沒有允許容量的條目。然后過濾到只有容量超過最大允許容量的結果。然后將該結果映射為所需的格式,其中鍵是ids,值是包含最大容量的錯誤資訊。
uj5u.com熱心網友回復:
1)你可以通過首先創建一個Map來輕松實作,如果id和capacity
const dict = new Map(allowedValues.map(({ id, capacity }) => [id, capacity]));
2)使用reduce在values陣列上回圈,以加工id并獲得結果
const values = [
{
id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
capacity: 8,
},
{
id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
capacity: 6,
},
{
id: "djkfu4-xvvxvx-4344-sddsd"/span>,
capacity: 1,
},
];
const allowedValues = [
{
id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d",
capacity: 3,
},
{
id: "be63bd14-f448-4dc4-8087-3b895095d0bc",
capacity: 2,
},
];
const dict = new Map( allowedValues. map(({ id, capacity }) => [id, capacity])。
const result = values.reduce((a, c) => {
if (dict.has(c.id) & & c. capacity > dict.get(c.id))
a.push({ [c.id] 。`Allowed capacity is ${dict.get(c.id)}` })。)
return a;
}, []);
console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
還有一種使用forEach的方法:
const values = [
{id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d", capacity: 8}。
{id: "be63bd14-f448-4dc4-8087-3b895095d0bc", capacity: 6}。
{id: "djkfu4-xvvxvx-4344-sddsd", capacity: 1}。
]
const allowedValues = [
{ id: "c22d8560-2d42-45c0-9ca0-91a4eabd193d", capacity: 3}。
{ id: "be63bd14-f448-4dc4-8087-3b895095d0bc",capacity: 2}。
]
let res = []
values.forEach(span class="hljs-params">v => {
allowedValues.forEach(a => {
v.capacity > a.capacity && res.push({[a.id] 。'允許的容量是'/span> a.capacity })
})
})
res = Array.from(new Set(res. map(JSON. stringify)).map(JSON.parse) 。)
console.log(res)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
完成這個任務的直接和描述性的方法是,從allowedValues陣列中創建一個查找表,然后針對該表減少資料陣列。
這里使用 Map 進行查找,并使用 for...of 回圈處理結果。
const values = [ { id: 'c22d8560-2d42-45c0-9ca0-91a4eabd193d', capacity: 8, }, { id: 'be63bd14-f448-4dc4-8087-3b895095d0bc', capacity: 6, }, { id: 'djkfu4-xvvxvx-4344-sddsd', capacity: 1, }, ] 。
const allowedValues = [ { id: 'c22d8560-2d42-45c0-9ca0-91a4eabd193d', capacity: 3, }, { id: 'be63bd14-f448-4dc4-8087-3b895095d0bc', capacity: 2, }, ] 。
const avMap = new Map( allowedValues. map(({ id, capacity }) => [id, capacity])。
const result = [];
for (const { id, capacity }) of values) {
if (avMap.has(id) && capacity > avMap.get(id) ) {
result.push({ [id]: `Allowed capacity is ${avMap.get(id)}` })。)
}
}
console.log(result);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/306937.html
標籤:
上一篇:不能將命令輸出發送到一個陣列中
