试图用一系列数字来过滤一系列对象,并找到我的索引和foreach无效



im试图过滤我的所有联系人,这些联系人与我要删除的数字数组。这应该在每个联系人上循环并删除不需要的数字。有些触点有两个数字,只能删除一个数字,但不能删除孔接触。我已经尝试过滤并查看所选的号码是否在索引中,但是Foreach似乎没有任何建议吗?我认为for for for for recto

const filteredContacts = contacts.filter(contact => numbers.indexOf(contact.phoneNumbers.forEach(phone => phone.number)) > -1);

//2 sample of all contacts
// contacts
  Object {
"company": "Financial Services Inc.",
"contactType": "person",
"firstName": "Hank",
"id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
"imageAvailable": false,
"jobTitle": "Portfolio Manager",
"lastName": "Zakroff",
"middleName": "M.",
"name": "Hank M. Zakroff",
"phoneNumbers": Array [
  Object {
    "countryCode": "us",
    "digits": "5557664823",
    "id": "337A78CC-C90A-46AF-8D4B-6CC43251AD1A",
    "label": "work",
    "number": "(555) 766-4823",
  },
  Object {
    "countryCode": "us",
    "digits": "7075551854",
    "id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
    "label": "other",
    "number": "(707) 555-1854",
  },
],
},
Object {
"contactType": "person",
"firstName": "David",
"id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
"imageAvailable": false,
"lastName": "Taylor",
"name": "David Taylor",
"phoneNumbers": Array [
  Object {
    "countryCode": "us",
    "digits": "5556106679",
    "id": "FE064E55-C246-45F0-9C48-822BF65B943F",
    "label": "home",
    "number": "555-610-6679",
  },
],
},
]

//numbers not to have
numbers = [
 5557664823,
 1344043005,
 5467865467,
]

//Expected
     Object {
"company": "Financial Services Inc.",
"contactType": "person",
"firstName": "Hank",
"id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
"imageAvailable": false,
"jobTitle": "Portfolio Manager",
"lastName": "Zakroff",
"middleName": "M.",
"name": "Hank M. Zakroff",
"phoneNumbers": Array [
  Object {
    "countryCode": "us",
    "digits": "7075551854",
    "id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
    "label": "other",
    "number": "(707) 555-1854",
  },
],
},
Object {
"contactType": "person",
"firstName": "David",
"id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
"imageAvailable": false,
"lastName": "Taylor",
"name": "David Taylor",
"phoneNumbers": Array [
  Object {
    "countryCode": "us",
    "digits": "5556106679",
    "id": "FE064E55-C246-45F0-9C48-822BF65B943F",
    "label": "home",
    "number": "555-610-6679",
  },
],
},
]

如果我理解正确,您想从联系人的电话号码中过滤一些numbers。可以使用 array.map()contacts数组上使用一个解决方案,然后用新的过滤阵列替换phoneNumbers数组。对于过滤过程,我们可以使用 array.filter() and array.includes()检查是否属于您要过滤的数字列表。

const contacts = [
  {
    "company": "Financial Services Inc.",
    "contactType": "person",
    "firstName": "Hank",
    "id": "2E73EE73-C03F-4D5F-B1E8-44E85A70F170",
    "imageAvailable": false,
    "jobTitle": "Portfolio Manager",
    "lastName": "Zakroff",
    "middleName": "M.",
    "name": "Hank M. Zakroff",
    "phoneNumbers": [
      {
        "countryCode": "us",
        "digits": "5557664823",
        "id": "337A78CC-C90A-46AF-8D4B-6CC43251AD1A",
        "label": "work",
        "number": "(555) 766-4823",
      },
      {
        "countryCode": "us",
        "digits": "7075551854",
        "id": "E998F7A3-CC3C-4CF1-BC21-A53682BC7C7A",
        "label": "other",
        "number": "(707) 555-1854",
      },
    ],
  },
  {
    "contactType": "person",
    "firstName": "David",
    "id": "E94CD15C-7964-4A9B-8AC4-10D7CFB791FD",
    "imageAvailable": false,
    "lastName": "Taylor",
    "name": "David Taylor",
    "phoneNumbers": [
      {
        "countryCode": "us",
        "digits": "5556106679",
        "id": "FE064E55-C246-45F0-9C48-822BF65B943F",
        "label": "home",
        "number": "555-610-6679",
      },
    ],
  },
];
// Numbers to filter out.
const numsToFilterOut = [5557664823];
let res = contacts.map(contact =>
{
    contact.phoneNumbers = contact.phoneNumbers.filter(
        // Unary plus is used to coerces (cast) the string to number.
        phone => !numsToFilterOut.includes(+phone.digits)
    );
    return contact;
});
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

相关内容

  • 没有找到相关文章

最新更新