如何根据其内容选择数组?

对于下面的示例,我想选择附件值,如果它的类型为“预期结算日期”?


我试过这样做:


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 };


一只名叫tom的猫
浏览 95回答 2
2回答

LEATH

使用Array#find:const {attachments} = state.form.conditions.find(({type})=>type==='Anticipated Settlement Date');

手掌心

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);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript