[英]Merge objects in javascript into one
My issue is that I have an initial object with data in the function.我的问题是我有一个初始object,其中包含 function 中的数据。 The function receives params with values that are into this initial object.
function 接收参数,其值包含在此初始object 中。 I need to update the initial object every time with the data, which comes from params .
我需要每次使用来自params的数据更新初始object 。
The code:编码:
export function saveLocalStorage(params = {}) {
let state = {
firstName: '',
lastName: '',
role: '',
idToken: '',
auth: false,
id: '',
email: '',
phone: '',
organizationId: '',
lastVisit: '',
}
localStorage.setItem('donisi-new', JSON.stringify(state))
}
params have the same names as names in initial object, example:参数名称与初始object 中的名称相同,例如:
saveLocalStorage({
firstName,
lastName,
role,
organizationId,
auth: true,
idToken,
lastVisit: moment(new Date()),
})
So, for example, the first time I received the first object with params, for example:因此,例如,我第一次收到带有参数的第一个 object,例如:
saveLocalStorage({
firstName: 'La La',
lastName: 'Bla Bla'
})
and second time I received object with params:我第二次收到带有参数的 object:
saveLocalStorage({
role: 'admin',
phone: '+111111111'
})
How to update the initial state and don't delete the values and only update them?如何更新初始state 并且不删除值而只更新它们?
Thanks to everybody.谢谢大家。
This is a function I use to merge 2 JavaScript objects:这是一个 function 我用来合并 2 个 JavaScript 对象:
function mergeObjects(obj, src) {
for (var key in src) {
if (src.hasOwnProperty(key)) obj[key] = src[key];
}
return obj;
}
So if you had these 2 objects:所以如果你有这两个对象:
var obj1 = {name: 'Bob', age: 30};
var obj2 = {name: 'Steve'};
And ran the function:并运行 function:
mergeObjects(obj1, obj2);
It would return:它会返回:
{name: 'Steve', age: 30}
To achieve this behaviour you can use the ES6's spread operator (...) to merge objects.要实现这种行为,您可以使用 ES6 的扩展运算符 (...) 来合并对象。 It will merge the two object.
它将合并两个 object。 The new fields will be added from both object and existing ones will be updated.
将从 object 中添加新字段,并且将更新现有字段。
Just replace your只需更换您的
localStorage.setItem('donisi-new', JSON.stringify(state))
with和
localStorage.setItem('donisi-new', JSON.stringify({...state, ...params}))
The order of state and params is important here. state 和参数的顺序在这里很重要。 This orders means state object will be updated with new values which exist in params object.
此订单意味着 state object 将使用参数 object 中存在的新值进行更新。
Part of the problem with updating initial state is you have state
defined in the function, so those values can't be updated.更新初始 state 的部分问题是您在
state
中定义了 state,因此无法更新这些值。 One way to address this is to pull state
out into its own file and then reference it in your function.解决此问题的一种方法是将
state
拉出到自己的文件中,然后在 function 中引用它。
// state.js
export default {
firstName: '',
lastName: '',
role: '',
idToken: '',
auth: false,
id: '',
email: '',
phone: '',
organizationId: '',
lastVisit: '',
};
Then in your function you can reference and update it as necessary.然后在您的 function 中,您可以根据需要引用和更新它。 The next time you call
saveLocalStorage
, the state will have been updated from the previous call.下次调用
saveLocalStorage
时,state 将从上一次调用中更新。
import * as state from "./state.js";
export function saveLocalStorage(params = {}) {
/* Update state with values from params example
for (const [key, value] of Object.entries(params)) {
state[key] = value;
}
*/
localStorage.setItem('donisi-new', JSON.stringify(state))
}
I leave part of this in a comment because you may have something else in mind for updating state or before merging.我将其中的一部分留在评论中,因为您可能有其他想法来更新 state 或在合并之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.