【问题标题】:adding another key and value to an object inside an array with JS使用JS向数组内的对象添加另一个键和值
【发布时间】:2021-08-26 11:53:36
【问题描述】:

所以我目前在一个数组中有一堆对象,如下所示。但是,我现在正在尝试编写一个函数,允许我将另一个键|值添加到最后添加的对象中。

我目前的想法是使用 arrayname.length - 1 来计算对象在数组中的位置。

我需要创建一个临时数组来存储新对象,然后在函数末尾设置 (tempArray = oldArray) 还是将它们都连接起来?

const state = [
{
    userId: 1,
},
{
    Name: name,
},
{
    age: 52,
},
{
    title: "et porro tempora",

}]

这是当前代码


let objects = [];


const addParent = (ev) =>{
  ev.preventDefault();
  // getting the length of the objects array
  let arrayLength = objects.length;
  // if the length of the array is zero - empty or one then set it to default zero
  // else if there is objects stored in the array minus 1 to get the array position
  if(arrayLength <= 0){
    arrayLength = 0;
  }else{
        arrayLength = objects.length - 1;
    }
  //make a temporary array to be able to push new parent into an existing object
  var tempObjects =  []
  for (var index=0; index<objects.length; index++){

  }
  //create a new parent object key : value
  let parent = {
    key: document.getElementById('key').value,
    value: document.getElementById('value').value
    }

//push parent object key and value into object
    //objects.push(parent);

}

document.addEventListener('DOMContentLoaded', () => {
    document.getElementById('btn1').addEventListener('click', addParent);
});

【问题讨论】:

  • 这能回答你的问题吗? Javascript ES6/ES5 find in array and change
  • 您的代码没有做任何事情。它实例化了几个变量,arrayLengthtempObjectsparent,但不对它们做任何事情,并且在执行块中有一个 for 循环,没有任何内容。请更新您的代码,以便清楚您要做什么。

标签: javascript


【解决方案1】:

有多种方法可以做到这一点。 试试这个

var objects = [{
  name: "1"
}];
const addParent = (ev) => {


  let parent = {
    key: "some value",
    value: "some value"
  }

  objects = Array.isArray(objects) ? objects : [];
  let lastObjectIndex = objects.length - 1;
  lastObjectIndex = lastObjectIndex > -1 ? lastObjectIndex : 0
  objects[lastObjectIndex] = { ...objects[lastObjectIndex],
    ...parent
  }



}

【讨论】:

    【解决方案2】:

    我认为您想为数组的最后一个对象添加新值

    方法一

    const state = [
    {
        userId: 1,
    },
    {
        Name: name,
    },
    {
        age: 52,
    },
    {
        title: "et porro tempora",
    
    }]
    
    
    state[state.length - 1].newKey = "value"
    
    console.log(state)

    方法二

    const state = [
    {
        userId: 1,
    },
    {
        Name: name,
    },
    {
        age: 52,
    },
    {
        title: "et porro tempora",
    
    }]
    
    // 1st method 
    
    state[state.length - 1] = {
     ...state[state.length - 1] ,
       newKey : "value"
     }
     console.log(state)

    【讨论】:

    • 如何在表单上获取两个用户输入,例如:let infoBoxOne = document.getElementById('key').value let infoBoxTwo = document.getElementById('value').value
    • 我听不懂你想说什么
    【解决方案3】:

    你可能会使用这样的东西:

    const a = [
      { "a" : "a" },
      { "b" : "b" }
      ]
      
    const c = a.map((obj, index) => {
      if (index === a.length -1) {
        return { ...obj, newProp: "newProp" }
        }
        return obj;
      });
      
    console.log(c)

    这将使用扩展运算符在最后一个对象上添加属性,如果您是 JS 新手,您可以查找它,但基本上它将保留所有现有属性并将newProp 添加到对象中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-25
      • 1970-01-01
      • 2021-07-01
      • 2017-07-17
      • 2021-12-28
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多