我正在使用 Javascript,并且有以下物件陣列:
const offersStep = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 5, price: 88.20},
{a: 7, price: 57.10},
{a: 13, price: 57.10},
{a: 15, price: 57.10},
{a: 29, price: 57.10},
{a: 30, price: 57.10},
]
為了將那些物件存盤在新陣列中,我必須做什么和如何做,而忽略那些道具price具有已存盤在新陣列中的值的物件?
預期輸出:
offers = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 7, price: 57.10}
]
uj5u.com熱心網友回復:
使用迭代來確定先前保存的價格是否與正在迭代的當前物件匹配。
const offersStep = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 5, price: 88.20},
{a: 7, price: 57.10},
{a: 13, price: 57.10},
{a: 15, price: 57.10},
{a: 29, price: 57.10},
{a: 30, price: 57.10},
]
// Vars to store previously recorded prices.
var storedPrices = [];
// Var to store unique offersStep.
var uniqueOffersStep = [];
// Iterates through each of the offersStep array objects.
offersStep.forEach (function(value) {
// Sets a boolean to false .. used later to determine if the current object is added to the unique array.
var isSkip = false;
// Iterates through each of the storedPrices array.
storedPrices.forEach (function(spvalue) {
// If the current objects price matches one of the storedPrices then it sets the skip flag to true.
if (value.price == spvalue) {
isSkip = true;
}
})
// Adds the current objects price to the storedPrices array.
storedPrices.push(value.price);
// Check if the skip flag is still false after checking stored prices and adds the object to the uniqueOffers variable.
if (isSkip == false) {
uniqueOffersStep.push(value);
}
})
// Outputs the final var to console.
console.log(uniqueOffersStep);
由于這依賴于多層迭代,因此使用較大的資料集將占用大量資源。
希望這可以幫助。
uj5u.com熱心網友回復:
您可以將物件存盤在與其price. 存盤唯一價格后,將 Map 的values()迭代器傳播到一個新陣列中以獲取結果。
const offersStep = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 5, price: 88.20},
{a: 7, price: 57.10},
{a: 13, price: 57.10},
{a: 15, price: 57.10},
{a: 29, price: 57.10},
{a: 30, price: 57.10},
];
function filterOffers(offers) {
let offersByPrice = new Map();
for (let offer of offers) {
if (!offersByPrice.has(offer.price)) {
// Store the offer
offersByPrice.set(offer.price, offer);
}
}
return [...offersByPrice.values()];
}
console.log(filterOffers(offersStep));
uj5u.com熱心網友回復:
主意
過濾原始陣列,通過一組來密切關注迄今為止遇到的價格。
代碼
const offersStep = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 5, price: 88.20},
{a: 7, price: 57.10},
{a: 13, price: 57.10},
{a: 15, price: 57.10},
{a: 29, price: 57.10},
{a: 30, price: 57.10}
];
let offer
, nset_prices = new Set()
;
offer = offersStep.filter ( po_item => {
let b_ok = !nset_prices.has(po_item.price)
;
if (b_ok) {
nset_prices.add(po_item.price)
}
return b_ok;
});
console.log(offer);
uj5u.com熱心網友回復:
如果代碼對您有用,請告訴我!
const offersStep = [
{ a: 1, price: 67.1 },
{ a: 3, price: 88.2 },
{ a: 5, price: 88.2 },
{ a: 7, price: 57.1 },
{ a: 13, price: 57.1 },
{ a: 15, price: 57.1 },
{ a: 29, price: 57.1 },
{ a: 30, price: 57.1 },
];
let tempOffers = {};
offersStep.forEach((eachObj) => {
const { price } = eachObj;
if (!tempOffers[price]) {
tempOffers[price] = eachObj;
}
});
const offers = Object.values(tempOffers);
console.log("offers are", offers);
uj5u.com熱心網友回復:
const newArray = offersStep.reduce((acc,curr) => {
if(acc.find(item => item.a === curr.a)) {
return acc
}
return acc.concat(curr)
},[])
uj5u.com熱心網友回復:
您可以使用一組價格進行過濾。
const
filterWithSet = s => ({ price }) => !s.has(price) && s.add(price),
offersStep = [{ a: 1, price: 67.10 }, { a: 3, price: 88.20 }, { a: 5, price: 88.20 }, { a: 7, price: 57.10 }, { a: 13, price: 57.10 }, { a: 15, price: 57.10 }, { a: 29, price: 57.10 }, { a: 30, price: 57.10 }],
result = offersStep.filter(filterWithSet(new Set));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
使用reduce、find和ternary運算子迭代給定陣列,檢查專案價格是否存在,如果找不到則附加到集合陣列:
const offersStep = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 5, price: 88.20},
{a: 7, price: 57.10},
{a: 13, price: 57.10},
{a: 15, price: 57.10},
{a: 29, price: 57.10},
{a: 30, price: 57.10},
]
const offers = offersStep.reduce(
(arr, item) => // collection array, and iteration item
// check collection array for item price
(arr.find(item2 => item2.price === item.price))
? arr // item price exists so return array unchanged
: [...arr, item], // return array with item appended
[] // initial collection array
);
console.log(offers);
uj5u.com熱心網友回復:
使用Array.Some
// data source
const offersStep = [
{a: 1, price: 67.10},
{a: 3, price: 88.20},
{a: 5, price: 88.20},
{a: 7, price: 57.10},
{a: 13, price: 57.10},
{a: 15, price: 57.10},
{a: 29, price: 57.10},
{a: 30, price: 57.10},
];
// this is the array to store new objects after dupliacte check
const offers =[];
offersStep.forEach(
(d,i)=>{
//push the first object from the array of objects
(i==0)?offers.push({a:d.a,price:d.price}):
//for the subsequent index, check offers if there exists at least one
// value for price that matches the current price from offersStep.price
// if yes, don't push, else push
offers.some(x=>x.price==d.price)===false?offers.push({a:d.a,price:d.price}):null
}
);
console.log(offers);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495966.html
標籤:javascript
上一篇:Javascript識別隨機位于陣列中的陣列元素的總和
下一篇:為什么我不能選中復選框?
