Как выбрать массив по его содержимому? - PullRequest
0 голосов
/ 13 июля 2020

В приведенном ниже примере я хотел бы выбрать значение вложений , если оно имеет тип «Ожидаемая дата расчетов»?

Я пробовал сделать это следующим образом:

state.form.conditions[[4]].attachments
var state = {
    form: {
        conditions: [{
            exists: '',
            attachments: [],
            type: 'Finance',
            description: '',
            status: 'In Progress',
            date: ''
        }, {
            exists: '',
            attachments: [],
            type: 'Valuation',
            description: '',
            status: 'In Progress',
            date: ''
        }, {
            exists: '',
            attachments: [],
            type: 'Inspection',
            description: '',
            status: 'In Progress',
            date: ''
        }, {
            exists: '',
            attachments: [],
            type: 'Other Sale',
            description: '',
            status: 'In Progress',
            date: ''
        }, {
            exists: 'true',
            **attachments: [],**
            type: 'Anticipated Settlement Date',
            description: '',
            status: 'In Progress',
            date: ''
        }],
        rejection_reason: '',
    },
    progress: false,
    editable: true,
    commercialLease: false,
    redirecting: false,
    formErrors: { }
};

export { state };

Ответы [ 2 ]

2 голосов
/ 13 июля 2020

Используйте Array#find:

const {attachments} = state.form.conditions.find(({type})=>type==='Anticipated Settlement Date');
1 голос
/ 13 июля 2020

Array.filter + Array.map - традиционный подход:

var state = {
  form: {
    conditions: [{
      exists: '',
      attachments: [],
      type: 'Finance',
      description: '',
      status: 'In Progress',
      date: ''
    }, {
      exists: '',
      attachments: [],
      type: 'Valuation',
      description: '',
      status: 'In Progress',
      date: ''
    }, {
      exists: '',
      attachments: [],
      type: 'Inspection',
      description: '',
      status: 'In Progress',
      date: ''
    }, {
      exists: '',
      attachments: [],
      type: 'Other Sale',
      description: '',
      status: 'In Progress',
      date: ''
    }, {
      exists: 'true',
      attachments: [ 'select me!' ],
      type: 'Anticipated Settlement Date',
      description: '',
      status: 'In Progress',
      date: ''
    }],
    rejection_reason: '',
  },
  progress: false,
  editable: true,
  commercialLease: false,
  redirecting: false,
  formErrors: {}
};

let sel = state
    .form
    .conditions
    .filter(item => item.type == 'Anticipated Settlement Date')
    .map(item => item.attachments);

console.log(sel);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...