在检查Javascript中的其他项目之前,请检查数组中的特定项目

时间:2019-05-13 12:47:55

标签: javascript arrays

我有一个array中的一个object,我想检查其中的object中的任何一个是否有标题为“食物”,但目前正在检查按顺序。贝娄是我的代码

let db = {
  users: [{
      id: 1,
      title: "food"
    },
    {
      id: 2,
      title: "stone"
    },
    {
      id: 3,
      title: "food"
    }
  ]
}

for (let index in db.users) {
  if (db.users[index].title === "food") {
    console.log("Its food");
    continue;
  }
  console.log("Its not food");
}

使用上面的代码,我得到以下结果

Its food
Its not food
Its food

我该怎么做,以便我先检查所有食物,然后获得结果

Its food
Its food
Its not food 

谢谢。

5 个答案:

答案 0 :(得分:2)

您可以将"food"个项目sort移到数组的开头,然后循环遍历

let db = { users: [ { id: 1, title: "food" },  { id: 2, title: "stone" },  { id: 3, title: "food" }] };

db.users.sort((a, b) => (b.title === "food") - (a.title === "food"))
        .forEach(a => console.log(a.title === "food" ? "It's food" : "It's not food"))

减去布尔值返回1,-1或0

true - false === 1
false - true === -1
true - true === 0

因此,如果a.title === foodb.title不是,-1将从compareFunction返回。因此,a将被放置在数组中b的前面。

答案 1 :(得分:1)

如果不是食物,您可以维护array并使用Array.push(最后添加),并使用Array.unshift(开头添加)添加是食物时的开始值。

let db = {users: [{id: 1,title: "food"},{id: 2,title: "stone"}, {id: 3,title: "food"}]};

let result = [];
for(let index in db.users) {
    if(db.users[index].title === "food") {
        result.unshift("Its food");
        continue;
    }
    result.push("Its not food");
}

console.log(result);

答案 2 :(得分:1)

如果您要先打印所有foods,然后再打印非食品项目,则可以使用两个不同的循环并分别打印

所有食物的第一个循环

for(let index in db.users) {
    if(db.users[index].title === "food") {
        console.log("Its food");
    }
}

所有非食物项目的循环秒数。

for(let index in db.users) {
    if(db.users[index].title != "food") {
        console.log("Its not food");
    }
}

为减少迭代次数,我们可以使用过滤器并分别保存过滤后的项目,然后遍历每个数组

let foodItems = db.users.filter(user => user.title === 'food')
let nonFoodItems = db.users.filter(user => user.title != 'food')

现在遍历根据标题过滤的每个数组

答案 3 :(得分:1)

您可以映射字符串,对数组进行排序和反转以按需要的顺序获取正确的文本。

let db = { users: [ { id: 1, title: "food" },  { id: 2, title: "stone" },  { id: 3, title: "food" }] }, 
    msg = db.users
        .map(({ title }) => title === 'food' ? "It's food" : "It isn't food")
        .sort()
        .reverse();

msg.forEach(m => console.log(m));

您可以使用生成器并按所需顺序获取值。

function* getInOrder([{ title }, ...rest]) {
    if (title === "food") {
        yield "It's food";
        if (rest.length) yield* getInOrder(rest);
    } else {
        if (rest.length) yield* getInOrder(rest);
        yield "It isn't food";
    }
}
let db = { users: [ { id: 1, title: "food" },  { id: 2, title: "stone" },  { id: 3, title: "food" }] };

[...getInOrder(db.users)].forEach(m => console.log(m));

答案 4 :(得分:1)

您可以使用Array.prototype.sort()方法对它们进行排序并映射到它们上

let db = {
    users: [
        {
           id: 1,
            title: "food"
        }, 
        {
            id: 2,
            title: "stone"
        }, 
        {
            id: 3,
            title: "food"
        }
    ]
}

const sortTitle = (a,b) => (a.title === "food" && b.title !== "food") ? -1 : 1;
const mapOutput = it => { if(it.title === "food") { console.log("Its food"); } else { console.log("Its not food");}};

db.users
.sort(sortTitle)
.map(mapOutput);