“JavaScript menggabungkan objek” Kode Jawaban

Objek klon di JS

var student = {name: "Rahul", age: "16", hobby: "football"};

//using ES6
var studentCopy1 = Object.assign({}, student);
//using spread syntax
var studentCopy2 = {...student}; 
//Fast cloning with data loss
var studentCopy3 = JSON.parse(JSON.stringify(student));
JavascriptNinja

Gabungkan dua objek JavaScript

const object1 = {
  name: 'Flavio'
}

const object2 = {
  age: 35
}
const object3 = {...object1, ...object2 } //{name: "Flavio", age: 35}
Batman

JavaScript menggabungkan objek

var person={"name":"Billy","age":34};
var clothing={"shoes":"nike","shirt":"long sleeve"};

var personWithClothes= Object.assign(person, clothing);//merge the two object
Grepper

Gabungkan objek JavaScript

const a = { b: 1, c: 2 };
const d = { e: 1, f: 2 };

const ad = { ...a, ...d }; // { b: 1, c: 2, e: 1, f: 2 }
PandaNïrio

cara menggabungkan dua objek menjadi satu di javascript

let obj1 = { foo: 'bar', x: 42 };
let obj2 = { foo: 'baz', y: 13 };

let clonedObj = { ...obj1 };
// Object { foo: "bar", x: 42 }

let mergedObj = { ...obj1, ...obj2 };
// Object { foo: "baz", x: 42, y: 13 }

Dangerous Dolphin

JavaScript menggabungkan objek

/**
 * Simple object check.
 * @param item
 * @returns {boolean}
 */
export function isObject(item) {
  return (item && typeof item === 'object' && !Array.isArray(item));
}

/**
 * Deep merge two objects.
 * @param target
 * @param ...sources
 */
export function mergeDeep(target, ...sources) {
  if (!sources.length) return target;
  const source = sources.shift();

  if (isObject(target) && isObject(source)) {
    for (const key in source) {
      if (isObject(source[key])) {
        if (!target[key]) Object.assign(target, { [key]: {} });
        mergeDeep(target[key], source[key]);
      } else {
        Object.assign(target, { [key]: source[key] });
      }
    }
  }

  return mergeDeep(target, ...sources);
}
Dotch

Jawaban yang mirip dengan “JavaScript menggabungkan objek”

Pertanyaan yang mirip dengan “JavaScript menggabungkan objek”

Lebih banyak jawaban terkait untuk “JavaScript menggabungkan objek” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya