简体   繁体   English

将javascript中的对象合并为一个

[英]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.

相关问题 javascript - 将对象数组中的 2 个对象合并为一个 Object - javascript - Merge 2 objects in Array of Objects into one Object 如何在一个对象(包括内部对象)中合并对象数组-JavaScript? - How to merge array of objects in one object, including inner objects - JavaScript? 将多个对象 arrays 合并为一个对象数组 JavaScript - Merge multiple arrays of objects into one array of objects JavaScript 如何合并子数组中的对象,以便在 javascript 中剩下一个数组和多个对象 - How to merge objects in a subArray such that there is one array left with multiple objects in javascript 将对象合并为一个并覆盖现有的 object - javascript es6 - merge objects into one and override the existing object - javascript es6 JavaScript 如果在一个对象数组中具有相同的 ID,则合并并加入 - JavaScript merge and join if same ID in one array of objects 将多个对象合并为一个 object 并组合属性 JavaScript - Merge several objects into one object and combine properties JavaScript 如何在 jQuery / javascript 中将多个对象合并为一个 object - how to merge multiple objects into one object in jQuery / javascript 比较两个数组对象数据并在JavaScript中将一个对象合并为一个数组? - Comparing two array objects data and merge as one object into array in javascript? 如何在 javascript 中的一个数组中合并另一个数组对象中的数组 - how to merge array inside another array objects in one array in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM