This question already has an answer here:
这个问题在这里已有答案:
- How do I remove a particular element from an array in JavaScript? 69 answers
如何从JavaScript中删除数组中的特定元素? 69个答案
can someone help me with some code to splice an array and push to new array. I have an arrays
有人可以帮助我使用一些代码来拼接数组并推送到新数组。我有一个阵列
someArray = [{
name: "Kristian",
lines: "2,5,10"
}, {
name: "John",
lines: "1,19,26,96"
}, {
name: "Kristian",
lines: "2,58,160"
}, {
name: "Felix",
lines: "1,19,26,96"
}];
i want to splice where name = to Kristian and push to a new array
我想拼接name = to Kristian并推送到一个新阵列
3 个解决方案
#1
1
You can use .reduce()
method along-with .splice()
:
您可以与.splice()一起使用.reduce()方法:
let data = [
{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"},
{name:"Kristian", lines:"2,58,160"}, {name:"Felix", lines:"1,19,26,96"}
];
let result = data.reduce((r, c, i, a) => {
if(c.name = 'Kristian')
r.push(a.splice(i, 1));
return r;
}, []);
console.log(result);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#2
1
You can use
您可以使用
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
filter()方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
The splice() method changes the contents of an array by removing existing elements and/or adding new elements.
splice()方法通过删除现有元素和/或添加新元素来更改数组的内容。
var someArray = [{name:"Kristian", lines:"2,5,10"},{name:"John", lines:"1,19,26,96"},{name:"Kristian", lines:"2,58,160"},{name:"Felix", lines:"1,19,26,96"}];
var res = someArray.filter((p,i) => {
if(p.name=="Kristian"){
someArray.splice(i, 1); //remove the mached object from the original array
return p;
}
});
console.log(res);
console.log('----------------');
console.log(someArray);
#3
0
You could use a mixture of filter
and map
:
您可以使用过滤器和地图的混合:
const filtered = someArray.filter(obj => obj.name !== "Kristian")
const newArray = someArray.map(obj => obj.name === "Kristian")
Filtered will remove the items whilst map will create a new array with the removed items
已过滤将删除项目,而地图将创建包含已删除项目的新数组
#1
1
You can use .reduce()
method along-with .splice()
:
您可以与.splice()一起使用.reduce()方法:
let data = [
{name:"Kristian", lines:"2,5,10"}, {name:"John", lines:"1,19,26,96"},
{name:"Kristian", lines:"2,58,160"}, {name:"Felix", lines:"1,19,26,96"}
];
let result = data.reduce((r, c, i, a) => {
if(c.name = 'Kristian')
r.push(a.splice(i, 1));
return r;
}, []);
console.log(result);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
#2
1
You can use
您可以使用
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
filter()方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
The splice() method changes the contents of an array by removing existing elements and/or adding new elements.
splice()方法通过删除现有元素和/或添加新元素来更改数组的内容。
var someArray = [{name:"Kristian", lines:"2,5,10"},{name:"John", lines:"1,19,26,96"},{name:"Kristian", lines:"2,58,160"},{name:"Felix", lines:"1,19,26,96"}];
var res = someArray.filter((p,i) => {
if(p.name=="Kristian"){
someArray.splice(i, 1); //remove the mached object from the original array
return p;
}
});
console.log(res);
console.log('----------------');
console.log(someArray);
#3
0
You could use a mixture of filter
and map
:
您可以使用过滤器和地图的混合:
const filtered = someArray.filter(obj => obj.name !== "Kristian")
const newArray = someArray.map(obj => obj.name === "Kristian")
Filtered will remove the items whilst map will create a new array with the removed items
已过滤将删除项目,而地图将创建包含已删除项目的新数组