为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

一只名叫tom的猫 2022-11-11 16:20:04
对于下面的示例,我想选择附件值,如果它的类型为“预期结算日期”?我试过这样做:state.form.conditions[[4]].attachmentsvar 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 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

使用Array#find

const {attachments} = state.form.conditions.find(({type})=>type==='Anticipated Settlement Date');


查看完整回答
反对 回复 2022-11-11
?
手掌心

TA贡献1942条经验 获得超3个赞

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


查看完整回答
反对 回复 2022-11-11
  • 2 回答
  • 0 关注
  • 69 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信