我如何在JavaScript中获取不匹配的字符串? [英] How could i get Not matched String in javascript?

查看:37
本文介绍了我如何在JavaScript中获取不匹配的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预订数组,我必须使用 searchValue 在数组中进行搜索.

I have a booking array and I have to search inside the array using searchValue .

在这里,我们必须检查预订ID字段.如果预订ID和 searchValue 匹配,我们必须将该对象推送到结果数组中.

here we have to check the booking id field. if booking id and searchValue matched we have to push that object into the result array.

工作代码

  1. 下面的结果数组示例

  1. result array example below

let searchValue ="12,13,15"

let searchValue = "12,13,15"

结果:

[{ name:"user 3", bookingid:12, product: "ui" },
    { name:"user 4", bookingid:13, product: "ef" }]

预期输出:

12,13在预订数组中匹配,因此我们必须获取NotMatchedsearchValue ="15"你能帮忙吗

12,13 matched in booking array so we have to get NotMatchedsearchValue = "15" could you please help here

let bookingArr = [
    { name:"user 1", bookingid:10, product: "ab" },
    { name:"user 1", bookingid:10, product: "cd" },

    { name:"user 2", bookingid:11, product: "ui" },
    { name:"user 1", bookingid:10, product: "ef" },

    { name:"user 3", bookingid:12, product: "ui" },
    { name:"user 4", bookingid:13, product: "ef" },
];
let searchValue = "12,13,15";
let set = new Set(searchValue.split(",").map(Number)); // for faster lookup
let res = bookingArr.filter(x => set.has(x.bookingid));
console.log(res);

// how can i get not matched searchValue 
// expected result notmatchedsearchValue ="15"

演示

推荐答案

与您以前的

The situation here is different when compared to your previous question. So, here you need to make a Set out bookingArr and not out of searchVal.

let bookingArr = [
  { name: "user 1", bookingid: 10, product: "ab" },
  { name: "user 1", bookingid: 10, product: "cd" },

  { name: "user 2", bookingid: 11, product: "ui" },
  { name: "user 1", bookingid: 10, product: "ef" },

  { name: "user 3", bookingid: 12, product: "ui" },
  { name: "user 4", bookingid: 13, product: "ef" },
];

let searchValue = "12,13,15";
let set = new Set(bookingArr.map((b) => b.bookingid));
let res = searchValue
  .split(",")
  .map(Number)
  .filter((s) => !set.has(s))
  .join();
console.log(res);

这篇关于我如何在JavaScript中获取不匹配的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆