简体   繁体   English

Javascript - 使用来自相同 object 的另一个值更新 object 属性

[英]Javascript - updating an object property with another value from the same object

I have the following object:我有以下 object:

{ id: 1, name: 'jdoe', currentDayHours: null, totalHours: [{ task: 'cleaning', hours: 10}, { task: 'reading', hours: 2 }]}

I am trying to create a function that will update the currentDayHours based on the task parameter passed to the function.我正在尝试创建一个 function,它将根据传递给 function 的任务参数更新currentDayHours So for example, if "cleaning" is passed to the function, the expected outcome of the object should be:因此,例如,如果将“清洁”传递给 function,则 object 的预期结果应该是:

{ id: 1, name: 'jdoe', currentDayHours: 10, totalHours: [{ task: 'cleaning', hours: 10}, { task: 'reading', hours: 2 }]}

I'm still new to javascript but I think I should use foreach and filter , but not sure how to use both with each other.我对 javascript 还是新手,但我认为我应该使用foreachfilter ,但不知道如何相互使用。 Any guidance would be appreciated!任何指导将不胜感激!

Direct property access is enough.直接访问财产就足够了。 Use Array.find to find the object.使用Array.find查找 object。

 data = { id: 1, name: 'jdoe', currentDayHours: null, totalHours: [{ task: 'cleaning', hours: 10}, { task: 'reading', hours: 2 }]} const updateHours = (data,key) => { data.currentDayHours = (data.totalHours.find(({task})=>task===key)||[]).hours return data } console.log(updateHours(data,'cleaning'))

You can use Array.find() to find the task object in the totalHours array, and then use that object's hours to assign to currentDayHours .您可以使用Array.find()totalHours数组中查找任务 object,然后使用该对象的hours分配给currentDayHours

If you want to modify the object in-place , you can do this:如果要就地修改 object ,可以这样做:

function updateCurrentDayHours(obj, taskName) {
    const task = obj.totalHours.find(t => t.task === taskName)
    if (task) obj.currentDayHours = task.hours
}

If you want to return a cloned object, you can use a deepClone function provided by some libaries like lodash.如果您想返回一个克隆的 object,您可以使用一些库(如 lodash)提供的 deepClone function。 Here I'm using JSON.parse() and JSON.stringify() for simplicity.在这里,为了简单起见,我使用JSON.parse()JSON.stringify()

function updateCurrentDayHours(obj, taskName) {
    const task = obj.totalHours.find(t => t.task === taskName)
    if (task) {
        const clone = JSON.parse(JSON.stringify(obj))
        clone.currentDayHours = task.hours
        return clone
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象属性键与另一个属性值 javaScript 相同 - Object property key same as another property value javaScript JavaScript:如果另一个对象在另一个属性中具有相同的值,则更改对象的值 - JavaScript: Changing object value if another object has the same value in another property 将JavaScript对象属性与同一JavaScript对象中的另一个属性进行映射 - Mapping JavaScript Object property with another property in same JavaScript Object 更新 JavaScript 对象属性 - Updating a JavaScript object property 更新javascript对象属性? - Updating javascript object property? 如何将 javascript 对象属性链接到同一对象的另一个属性? - How to link a javascript object property to another property of the same object? 当输入是属性值 Javascript 数组时,从 Array 对象中删除对象具有相同的属性值 - Remove Object have same property value from Array object when input is Array of property value Javascript 将一个对象的属性值设置为等于另一个对象中的相同属性 - Set the property value of an object to be equal to the same property in another object 当我在同一对象中拥有另一个属性的值时,从对象数组中获取一个Object属性 - Get a Object property from array of objects when I have the value of another property in the same object Javascript用另一个对象更新对象 - Javascript updating object with another object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM