我需要从一个数组中删除一项,然后将其放入另一个数组的顶部。
我目前有一系列文章,其中一些文章的类型是“英雄”是真实的,其他的只是常规的。我需要找到数组中的第一篇英雄文章并将其删除。然后把这篇文章放到另一个数组的最上面。任何帮助将非常感激。谢谢
我目前有这个:
articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 3", hero: true},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
]
但想要这个结果:
articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
]
hero = [
{title: "article 3", hero: true}
]
.findIndex()
来获取英雄等于true
的第一篇文章。然后,您可以使用此索引将对象添加到您的 hero
数组中,然后再次使用它通过使用 articles
从您的
.splice()
数组中删除。
参见下面的示例:
const articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 3", hero: true},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
];
const hero = [];
const foundArticle = articles.findIndex(({hero}) => hero);
hero.push(articles[foundArticle]);
articles.splice(foundArticle, 1);
console.log(articles);
console.log(hero);
对于 IE 支持,您可以手动查找索引,而不是使用
.findIndex()
:
function findObjInArr(arr, cb) {
for(let i = 0; i < arr.length; i++) {
let obj = arr[i];
if(cb(obj)) {
return i;
}
}
return -1;
}
const articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 3", hero: true},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
];
const hero = [];
const foundArticle = findObjInArr(articles, function(obj) {
return obj.hero;
});
hero.push(articles[foundArticle]);
articles.splice(foundArticle, 1);
console.log(articles);
console.log(hero);
试试这个:
const articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 3", hero: true},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true},
];
const heros = [];
for(let [i, article] of articles.entries())
{
if(article.hero)
{
heros.unshift(article);
articles.splice(i, 1);
break;
}
}
console.log(heros);
console.log(articles);
var articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 3", hero: true},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
];
// Get the index of the first hero article, findIndex is not supported by IE
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex#Browser_compatibility
var heroIndex = articles.findIndex(function (article) {
return article.hero;
});
var hero = [];
if (heroIndex > -1) {
// remove the first hero article from articles and add it to a new array
hero = articles.splice(heroIndex, 1);
}
console.log("Hero article", hero);
console.log("Other articles", articles);
你可以尝试,
let articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 3", hero: true},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
];
let hero = [];
let heroArticle = {};
for(let article of articles){
if(article.hero){
heroArticle = article;
hero.push(article);
break;
}
}
articles = articles.filter(a => a.title !== heroArticle.title);
console.log(articles);
articles = [
{title: "article 1", hero: false},
{title: "article 2", hero: false},
{title: "article 4", hero: false},
{title: "article 5", hero: true},
{title: "article 6", hero: false},
{title: "article 7", hero: true}
]
result = []
for(var article of articles) {
if (article.hero) {
articles.splice(articles.indexOf(article))
result.push(article)
}
}
console.log(articles)
console.log(result)
var hero = []
var founds = articles.find((e)=>{
return e.hero ? e: false
})
hero.unshift(founds)
首先,选择文章数组的第三个元素 -
文章[2];
定义一个空数组名称 Hero -
让 Hero = [] 或 Hero = new Array();
然后按-
hero.push(文章[2]);