我正在嘗試拼接parcel物件內陣列中的所有匹配項。
稱為物件陣列的示例documents:
[
{name: "first", content: ["1", "2", "1", "3"]},
{name: "second", content: ["2", "1", "1", "1"]},
{name: "third", content: ["4", "1", "3", "2"]},
]
parcel = 1我正在嘗試拼接1.
這是我目前所擁有的:
console.log(parcel);
const parcel = 1;
console.log("doc start", documents);
for (i of documents) {
// get index by count
let count = -1;
for (j of i.content) {
count = count 1;
console.log(j);
if (j == parcel) {
i.content.splice(count, 1);
}
}
}
console.log("doc end", documents);
這是控制臺使用不同陣列示例記錄的內容:
parcel 1
doc start [
{
title: '1',
content: [
'1', '2', '2',
'3', '1', '1',
'2', '3'
]
}
]
1
2
3
1
2
3
doc end [ { title: '1', content: [ '2', '2', '3', '1', '2', '3' ] } ]
您可以看到j似乎沒有回圈遍歷整個 of i.content,這意味著該if陳述句沒有運行以拼接所有正確的專案。
控制臺日志的最終結果應該是:
doc start [{title: '1', content: [ '2', '2', '3', '2', '3' ]}]
我不確定為什么會這樣……
uj5u.com熱心網友回復:
以下是如何map和filter可能的作業方式。對于迭代中的每個物件,使用擴展語法1創建一個新物件,并通過從現有陣列中過濾掉所有 s 來創建一個新的內容陣列。
const data=[{name:"first",content:["1","2","1","3"]},{name:"second",content:["2","1","1","1"]},{name:"third",content:["4","1","3","2"]}];
const out = data.map(obj => {
return {
...obj,
content: obj.content.filter(n => Number(n) !== 1)
};
});
console.log(out);
uj5u.com熱心網友回復:
由于您只想按內容過濾,因此您應該使用map和filter內置陣列方法
const parcel = 1
const result = [
{name: "first", content: ["1", "2", "1", "3"]},
{name: "second", content: ["2", "1", "1", "1"]},
{name: "third", content: ["4", "1", "3", "2"]},
].map(item =>({...item, content:item.content.filter(amount => Number(amount) !== parcel)}))
uj5u.com熱心網友回復:
如果你想從一個你正在迭代的陣列中洗掉多個專案,你可能會得到意想不到的結果,這取決于for-in/for-of或array.forEach()作業方式的方式。
它們都像 -loop 一樣遍歷陣列for(;;),無論如何您都只需增加索引即可。
它們都類似于:
for (let idx = 0; idx < array.length; idx ) {
// logic goes here
}
但是,如果您使用 洗掉索引 1 處array.splice()的專案,則先前位于索引 2 處的專案將變為索引 1 處的專案,所有后續專案也將其索引減少 1。因此,將跳過先前索引 2 的專案。
有關更多資訊,您可以查看陳述句的規范for-of。
在此示例中,您可以看到索引 3(值 4)處的專案被跳過。
const arr = [1,2,3,4,5,6];
let idx = 0;
for (const item of arr) {
console.log(idx, item);
if (item == 3) {
arr.splice(idx,1);
}
idx ;
}
console.log(arr);
如果你想改變陣列,有幾種方法可以使用。
- Array.prototype.reduceRight(callback, initialValue)
- for/while 回圈
- 如果沒有任何更改,只會增加索引
- 從最后一個開始減少索引
- 使用
Object.keys(obj).reverse()或Object.entries(obj).reverse()Object.entries(array).reverse().forEach(([idx, item]) => { /* splice logic goes here */ })
注意: Array.prototype.filter僅當對陣列的參考未存盤在其他任何地方時才應使用。
例子:
使用 while 或 for 回圈,只有在沒有洗掉任何內容時才會增加索引。
const arr = [1,2,3,4,5,6];
for (let idx = 0; idx < arr.length;) {
let item = arr[idx];
console.log(idx, item);
if (item == 3) {
arr.splice(idx, 1);
} else {
idx ;
}
}
console.log(arr);
向后遍歷陣列的索引號。
const arr = [1,2,3,4,5,6];
for (let idx = arr.length - 1; idx > -1; --idx) {
let item = arr[idx];
console.log(idx, item);
if (item == 3) {
arr.splice(idx, 1);
}
}
console.log(arr);
With array.reduceRight(),它也從最高的專案/索引開始:
從檔案:
initialValue用作第一次呼叫 callbackFn 的累加器的值。如果未提供初始值,則將使用并跳過陣列中的最后一個元素。在沒有初始值的空陣列上呼叫 reduce 或 reduceRight 會創建 TypeError。
const arr = [1,2,3,4,5,6];
// only if the array is not empty
// acc or return value is not needed
arr.length && arr.reduceRight((acc, item, idx) => {
console.log(idx, item);
if (item == 3) {
arr.splice(idx, 1);
}
// can be anything, but must be present
}, "unused Initial Value");
console.log(arr);
根據您使用 Object.entries() 的示例:
const array = [
{ name: "first", content: ["1", "2", "1", "3"] },
{ name: "second", content: ["2", "1", "1", "1"] },
{ name: "third", content: ["4", "1", "3", "2"] },
];
for (const { content } of array) {
if (Array.isArray(content)) {
Object.entries(content).reverse().forEach(([idx, item]) => {
if (item === "1") {
content.splice(idx, 1);
}
})
}
}
console.log(array);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525182.html
上一篇:R編程-回文
