【问题标题】:Replace existing object in array with new value用新值替换数组中的现有对象
【发布时间】:2020-08-05 22:26:09
【问题描述】:

我有多个下拉菜单,我正在尝试更新数组中存在的对象的值

我的onChange如下

          onChange={e => {
            const answer = {
              id: questionChoice.id,
              name: questionChoice.text,
              value: e,
            }
            let newAnswer = {
              ...currentAnswer,
            }

            if (!currentAnswer.value) {
              newAnswer.value = [] // init empty array
            }

            if (index in newAnswer.value) {
              console.log('found')
              console.log('here', index in newAnswer.value)
              newAnswer.value = newAnswer.value.filter(item => item.value !== answer.value)
            }

            newAnswer.value = [...newAnswer.value, { [questionChoice.text]: answer.value }]
            console.log('new answer', newAnswer)

            updateCurrent(newAnswer)
          }}
          optionFilterProp="children"
          onFocus={onFocus}
          onBlur={onBlur}
          filterOption={(input, option) =>
            option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
          }
        >
         

我目前的输出是这样的

newAnswer.value: [
0: {Inform producers about the range of sustainability practices: "1"}
1: {Inform producers about environmental management: "7"}
2: {Provide mentoring services to new advisors: "12"}
3: {Inform producers about the range of sustainability practices: "2"}
]

预期的输出是

newAnswer.value: [
0: {Inform producers about the range of sustainability practices: "2"}
1: {Inform producers about environmental management: "7"}
2: {Provide mentoring services to new advisors: "12"}
]

现在它正在附加来自 newAnswer.value = [...newAnswer.value, { [questionChoice.text]: answer.value }] 的任何新结果

但是我找到了正确的对象

if (index in newAnswer.value) {
                  console.log('found')
                  // replace object here
                  newAnswer.value = newAnswer.value.filter(item => item.value !== answer.value)
               }

只是不确定如何就地更改特定对象

【问题讨论】:

  • 您的输出不是任何有效的已知格式
  • 它是一个对象数组?
  • 什么是index,它来自哪里,为什么要使用in 运算符?
  • 至少:if (index in newAnswer.value) { 是错误的,因为index 是全局的。您需要先将索引声明为本地,通常在其前面加上let
  • 然而,你实际上并没有使用index,所以至少这是可疑的。

标签: javascript oop ecmascript-6


【解决方案1】:

您可以使用Array#find 查找对象并在之后更新属性值。

const value = [{
  'Inform producers about the range of sustainability practices': "1"
}, {
  'Inform producers about environmental management': "7"
}, {
  'Provide mentoring services to new advisors': "12"
}];
let questionChoice = 'Inform producers about the range of sustainability practices';
let newAnswer = '2';
let obj = value.find(o => o.hasOwnProperty(questionChoice));
if(obj != null) obj[questionChoice] = newAnswer;
console.log(value);

【讨论】:

    【解决方案2】:

    想通了

    改变了

    newAnswer.value = [...newAnswer.value, { [questionChoice.text]: answer.value }]

    newAnswer.value[index] = { [questionChoice.text]: answer.value }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 2020-05-23
      • 2020-09-29
      • 2021-01-22
      • 2016-07-20
      相关资源
      最近更新 更多