Je dois supprimer un élément d'un tableau, puis le placer dans le haut d'un autre tableau.
J'ai actuellement un éventail d'articles, certains de ces articles ont le type hero est vrai et les autres sont juste réguliers. Je dois trouver le premier article de héros dans le tableau et le supprimer. Mettez ensuite cet article en haut d'un autre tableau. Toute aide serait très appréciée. Merci
J'ai actuellement ceci:
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}
]
Mais je veux ce résultat:
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}
]
7 réponses
Essaye ça:
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);
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)
Vous pouvez essayer comme,
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);
Comme ça?
var hero = []
var founds = articles.find((e)=>{
return e.hero ? e: false
})
hero.unshift(founds)
Vous pouvez utiliser .findIndex()
pour obtenir le premier article avec un héros égal à true
. Vous pouvez ensuite utiliser cet index pour ajouter l'objet à votre tableau hero
, puis le réutiliser pour le supprimer de votre tableau articles
en utilisant .splice()
.
Voir l'exemple ci-dessous:
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);
Pour la prise en charge d'IE, vous pouvez rechercher manuellement l'index au lieu d'utiliser .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);
Tout d'abord, choisissez le troisième élément du tableau d'articles -
Articles [2];
Définir un nom de tableau vide Hero -
Laissez Hero = [] ou Hero = new Array ();
Puis poussez -
Hero.push (articles [2]);
Questions connexes
De nouvelles questions
javascript
Pour des questions concernant la programmation dans ECMAScript (JavaScript / JS) et ses divers dialectes / implémentations (hors ActionScript). Veuillez inclure toutes les balises pertinentes dans votre question; par exemple, [node.js], [jquery], [json], etc.