Angular сравнивает строку с данными в объекте

#angular #typescript

#angular #typescript

Вопрос:

ВСЕ.

У меня вопрос. Каков наилучший способ сравнения данных.

Например, у вас есть некоторый постоянный ответ = 225235743;

И я хочу найти и отобразить все данные в объекте с тем же идентификатором, что и в этом ответе.

Нужно решить это в typescript, а не HTML с for, if и т.д…

    {
  "data" : [
    {
      "L_PHONE_NUMBER" : "bb",
      "L_DELETED" : "",
      "UID" : 1,
      "L_ADDRESS" : "addressb",
      "L_NIGHT_HOURS_TO" : "",
      "L_ACCOUNT_ID" : "225235743",
      "L_NIGHT_HOURS_FROM" : "",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "Branch 1b",
      "L_ID" : "arbvgBwzNc",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : "bb"
    },
    {
      "L_PHONE_NUMBER" : "8885577910",
      "L_DELETED" : "",
      "UID" : 2,
      "L_ADDRESS" : "640 S Hathaway St, Santa Ana, CA 92705",
      "L_NIGHT_HOURS_TO" : "12:00:00",
      "L_ACCOUNT_ID" : "syWESGISHx",
      "L_NIGHT_HOURS_FROM" : "00:00:00",
      "L_DEALER_ID" : "QvUuWqPhRG",
      "L_BRANCH_NAME" : "Santa Ana, S Hathaway St",
      "L_ID" : "Dmx3ma5dnI",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : "15"
    },
    {
      "L_PHONE_NUMBER" : "(310)-541-6110",
      "L_DELETED" : "",
      "UID" : 4,
      "L_ADDRESS" : "745 W La Cadena Dr, Riverside CA 925012",
      "L_NIGHT_HOURS_TO" : "00:00:00",
      "L_ACCOUNT_ID" : "225235743",
      "L_NIGHT_HOURS_FROM" : "12:00:00",
      "L_DEALER_ID" : "cDsUw0Bl44",
      "L_BRANCH_NAME" : "Riverside Branch222",
      "L_ID" : "M3Y9FVnRAm",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : "10"
    },
    {
      "L_PHONE_NUMBER" : "(310)555-6667",
      "L_DELETED" : "",
      "UID" : 5,
      "L_ADDRESS" : "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO" : "",
      "L_ACCOUNT_ID" : "225235743",
      "L_NIGHT_HOURS_FROM" : "",
      "L_DEALER_ID" : "ePtEH1ClEu",
      "L_BRANCH_NAME" : "Luis's Branch",
      "L_ID" : "5i3u4YubdJ",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : ""
    },
    {
      "L_PHONE_NUMBER" : "123",
      "L_DELETED" : "",
      "UID" : 7,
      "L_ADDRESS" : "nope",
      "L_NIGHT_HOURS_TO" : "11:00:00",
      "L_ACCOUNT_ID" : "",
      "L_NIGHT_HOURS_FROM" : "10:00:00",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "test",
      "L_ID" : "",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : "15"
    },
    {
      "L_PHONE_NUMBER" : "123",
      "L_DELETED" : "",
      "UID" : 9,
      "L_ADDRESS" : "nope",
      "L_NIGHT_HOURS_TO" : "11:00:00",
      "L_ACCOUNT_ID" : "",
      "L_NIGHT_HOURS_FROM" : "10:00:00",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "test",
      "L_ID" : "",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : "15"
    },
    {
      "L_PHONE_NUMBER" : "8885577910",
      "L_DELETED" : "",
      "UID" : 11,
      "L_ADDRESS" : "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO" : "",
      "L_ACCOUNT_ID" : "",
      "L_NIGHT_HOURS_FROM" : "",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "",
      "L_ID" : "",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : ""
    },
    {
      "L_PHONE_NUMBER" : "8885577910",
      "L_DELETED" : "",
      "UID" : 12,
      "L_ADDRESS" : "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO" : "",
      "L_ACCOUNT_ID" : "",
      "L_NIGHT_HOURS_FROM" : "",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "",
      "L_ID" : "",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : ""
    },
    {
      "L_PHONE_NUMBER" : "8885577910",
      "L_DELETED" : "",
      "UID" : 14,
      "L_ADDRESS" : "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO" : "",
      "L_ACCOUNT_ID" : "",
      "L_NIGHT_HOURS_FROM" : "",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "name",
      "L_ID" : "",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : ""
    },
    {
      "L_PHONE_NUMBER" : "8885577910",
      "L_DELETED" : "",
      "UID" : 15,
      "L_ADDRESS" : "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO" : "",
      "L_ACCOUNT_ID" : "",
      "L_NIGHT_HOURS_FROM" : "",
      "L_DEALER_ID" : "",
      "L_BRANCH_NAME" : "Santa Ana, S Hathaway St",
      "L_ID" : "",
      "L_REGION_ID" : "",
      "L_JOB_DELAY" : ""
    }
  ],
  "offset" : 0,
  "size" : 15,
  "dataSet" : {
    "localization" : { },
    "code" : "123123123",
    "name" : "3213213123",
    "lastChange" : "Thu, 20 Aug 2020 14:18:57 GMT",
    "id" : "7"
  }
}
  

Заранее приношу извинения за глупый вопрос, но я долгое время думал об этом решении.

Комментарии:

1. предполагая, что у вас есть JSON в переменных данных . const result = data.data.filter(d => d.L_ACCOUNT_ID === response) ;

Ответ №1:

Вы можете использовать метод filter для поиска данных с одинаковым идентификатором

 const response = 225235743;

const data = {
  "data": [{
      "L_PHONE_NUMBER": "bb",
      "L_DELETED": "",
      "UID": 1,
      "L_ADDRESS": "addressb",
      "L_NIGHT_HOURS_TO": "",
      "L_ACCOUNT_ID": "225235743",
      "L_NIGHT_HOURS_FROM": "",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "Branch 1b",
      "L_ID": "arbvgBwzNc",
      "L_REGION_ID": "",
      "L_JOB_DELAY": "bb"
    },
    {
      "L_PHONE_NUMBER": "8885577910",
      "L_DELETED": "",
      "UID": 2,
      "L_ADDRESS": "640 S Hathaway St, Santa Ana, CA 92705",
      "L_NIGHT_HOURS_TO": "12:00:00",
      "L_ACCOUNT_ID": "syWESGISHx",
      "L_NIGHT_HOURS_FROM": "00:00:00",
      "L_DEALER_ID": "QvUuWqPhRG",
      "L_BRANCH_NAME": "Santa Ana, S Hathaway St",
      "L_ID": "Dmx3ma5dnI",
      "L_REGION_ID": "",
      "L_JOB_DELAY": "15"
    },
    {
      "L_PHONE_NUMBER": "(310)-541-6110",
      "L_DELETED": "",
      "UID": 4,
      "L_ADDRESS": "745 W La Cadena Dr, Riverside CA 925012",
      "L_NIGHT_HOURS_TO": "00:00:00",
      "L_ACCOUNT_ID": "225235743",
      "L_NIGHT_HOURS_FROM": "12:00:00",
      "L_DEALER_ID": "cDsUw0Bl44",
      "L_BRANCH_NAME": "Riverside Branch222",
      "L_ID": "M3Y9FVnRAm",
      "L_REGION_ID": "",
      "L_JOB_DELAY": "10"
    },
    {
      "L_PHONE_NUMBER": "(310)555-6667",
      "L_DELETED": "",
      "UID": 5,
      "L_ADDRESS": "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO": "",
      "L_ACCOUNT_ID": "225235743",
      "L_NIGHT_HOURS_FROM": "",
      "L_DEALER_ID": "ePtEH1ClEu",
      "L_BRANCH_NAME": "Luis's Branch",
      "L_ID": "5i3u4YubdJ",
      "L_REGION_ID": "",
      "L_JOB_DELAY": ""
    },
    {
      "L_PHONE_NUMBER": "123",
      "L_DELETED": "",
      "UID": 7,
      "L_ADDRESS": "nope",
      "L_NIGHT_HOURS_TO": "11:00:00",
      "L_ACCOUNT_ID": "",
      "L_NIGHT_HOURS_FROM": "10:00:00",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "test",
      "L_ID": "",
      "L_REGION_ID": "",
      "L_JOB_DELAY": "15"
    },
    {
      "L_PHONE_NUMBER": "123",
      "L_DELETED": "",
      "UID": 9,
      "L_ADDRESS": "nope",
      "L_NIGHT_HOURS_TO": "11:00:00",
      "L_ACCOUNT_ID": "",
      "L_NIGHT_HOURS_FROM": "10:00:00",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "test",
      "L_ID": "",
      "L_REGION_ID": "",
      "L_JOB_DELAY": "15"
    },
    {
      "L_PHONE_NUMBER": "8885577910",
      "L_DELETED": "",
      "UID": 11,
      "L_ADDRESS": "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO": "",
      "L_ACCOUNT_ID": "",
      "L_NIGHT_HOURS_FROM": "",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "",
      "L_ID": "",
      "L_REGION_ID": "",
      "L_JOB_DELAY": ""
    },
    {
      "L_PHONE_NUMBER": "8885577910",
      "L_DELETED": "",
      "UID": 12,
      "L_ADDRESS": "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO": "",
      "L_ACCOUNT_ID": "",
      "L_NIGHT_HOURS_FROM": "",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "",
      "L_ID": "",
      "L_REGION_ID": "",
      "L_JOB_DELAY": ""
    },
    {
      "L_PHONE_NUMBER": "8885577910",
      "L_DELETED": "",
      "UID": 14,
      "L_ADDRESS": "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO": "",
      "L_ACCOUNT_ID": "",
      "L_NIGHT_HOURS_FROM": "",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "name",
      "L_ID": "",
      "L_REGION_ID": "",
      "L_JOB_DELAY": ""
    },
    {
      "L_PHONE_NUMBER": "8885577910",
      "L_DELETED": "",
      "UID": 15,
      "L_ADDRESS": "640 S Hawasqy St, Sala mana, FA 3123123",
      "L_NIGHT_HOURS_TO": "",
      "L_ACCOUNT_ID": "",
      "L_NIGHT_HOURS_FROM": "",
      "L_DEALER_ID": "",
      "L_BRANCH_NAME": "Santa Ana, S Hathaway St",
      "L_ID": "",
      "L_REGION_ID": "",
      "L_JOB_DELAY": ""
    }
  ],
  "offset": 0,
  "size": 15,
  "dataSet": {
    "localization": {},
    "code": "123123123",
    "name": "3213213123",
    "lastChange": "Thu, 20 Aug 2020 14:18:57 GMT",
    "id": "7"
  }
};
const result = data.data.filter(d => d.L_ACCOUNT_ID == response);
console.log(result);  

Комментарии:

1. спасибо, это то, что мне нужно. Я пробовал аналогичное решение, но я добавил if, и это было проблемой. this.data.data.filter((x) => { if (res === x.L_ACCOUNT_ID) { } Нужно подождать 4 минуты, и мы примем это как ответ.

2. Да. У вас несоответствие типов данных.. L_ACCOUNT_ID находится в строке, а response имеет тип number.. === также проверяет наличие типов