戻る

配列要素の削除
配列の指定したインデックス要素の削除をする処理

[サンプル]

copy
let ary=[1,2,3,4,5];
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
let removeIndex=1;
ary=ary.filter((_,i)=>i!== removeIndex);
console.log("[result]");
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
[結果]
i:0 ary:1
i:1 ary:2
i:2 ary:3
i:3 ary:4
i:4 ary:5
[result]
i:0 ary:1
i:1 ary:3
i:2 ary:4
i:3 ary:5

ary=ary.filter((_,i)=>i!== removeIndex);
filterメソッドを使い、削除したいインデックスしたいデータを除くデータをary配列に入れ直しています。
removeIndexには1を指定しています。
配列インデックスは0から始まります。
filterの引数に使用している「i」がインデックスと一致するremoveIndexが削除対象となります。
したがって2番目の要素を削除対象としています。

copy
let ary=[1,2,3,4,5];
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
let removeArray=[2,4];
ary=ary.filter((_,i) => !removeArray.includes(i));
console.log("[result]");
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
[結果]
i:0 ary:1
i:1 ary:2
i:2 ary:3
i:3 ary:4
i:4 ary:5
[result]
i:0 ary:1
i:1 ary:2
i:2 ary:4

ary=ary.filter((_,i) => !removeArray.includes(i));
複数の削除したいデータを指定するため配列を準備します。
この例ではremoveArray配列です。
配列に格納されているデータと一致する要素は除外対象となります。
削除対象の配列には、削除したいインデックス位置を指定します。
filterの引数に使用している「i」と一致するremoveArray配列のインデックスが削除対象となります。
したがって3番目と5番目のデータを削除します。



戻る
back

Deleting an array element
Deletes the specified index element from the array.

[sample]

copy
let ary=[1,2,3,4,5];
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
let removeIndex=1;
ary=ary.filter((_,i)=>i!== removeIndex);
console.log("[result]");
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
[result]
i:0 ary:1
i:1 ary:2
i:2 ary:3
i:3 ary:4
i:4 ary:5
[result]
i:0 ary:1
i:1 ary:3
i:2 ary:4
i:3 ary:5

ary=ary.filter((_,i)=>i!== removeIndex);
The filter method is used to repopulate the ary array with the data excluding the indexed data to be deleted.
The removeIndex is set to 1.
Array indexes start from 0.
The removeIndex whose index matches the "i" used in the filter argument will be the element to be deleted.
Therefore, the second element is the element to be deleted.

copy
let ary=[1,2,3,4,5];
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
let removeArray=[2,4];
ary=ary.filter((_,i) => !removeArray.includes(i));
console.log("[result]");
for(i=0; i < ary.length; i++)
{
	console.log("i:"+i+" ary:"+ary[i]);
}
[result]
i:0 ary:1
i:1 ary:2
i:2 ary:3
i:3 ary:4
i:4 ary:5
[result]
i:0 ary:1
i:1 ary:2
i:2 ary:4

ary=ary.filter((_,i) => !removeArray.includes(i));
Prepare an array to specify multiple pieces of data to delete.
In this example, use the removeArray array.
Elements that match the data stored in the array will be excluded.
Specify the index position of the array to delete.
The index in the removeArray array that matches the "i" argument used in filter will be deleted.
Therefore, the third and fifth pieces of data will be deleted.



back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。