将数组中的项目移到javascript中的顶部

时间:2019-06-19 10:06:23

标签: javascript arrays

我需要从一个数组中删除一项,然后将其放入另一个数组的顶部。

我目前有一系列文章,其中一些文章的类型是hero,而其他只是常规的。我需要在数组中找到第一篇英雄文章并将其删除。然后将本文放在另一个数组的顶部。任何帮助将非常感激。谢谢

我目前有这个:

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}
]

8 个答案:

答案 0 :(得分:4)

您可以使用.findIndex()获得第一篇英雄等于true的文章。然后,您可以使用该索引将对象添加到hero数组中,然后通过使用.splice()再次使用它从articles数组中删除。

请参见以下示例:

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);

答案 1 :(得分: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 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);

答案 2 :(得分:1)

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);

答案 3 :(得分:0)

您可以使用以下代码删除,只需为x添加元素的位置即可。

articles.splice(x, 1);

使用以下代码,您可以将其添加到新数组中:

articles.push('New Element');

答案 4 :(得分:0)

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)

答案 5 :(得分:0)

您可以尝试

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);

答案 6 :(得分:0)

像这样吗?

var hero = []
var founds = articles.find((e)=>{
  return e.hero ? e: false
})
hero.unshift(founds)

答案 7 :(得分:-3)

首先,为articles数组选择第三个元素-

文章[2];

定义一个空数组名称Hero-

让Hero = []或Hero = new Array();

然后推送-

hero.push(articles [2]);