覆盖对象的所有键,但其ID

时间:2018-10-12 08:08:26

标签: javascript

我有一个与此对象相似的对象,但是它具有更多的键。我想更新其所有键,但ID。我可以手动操作。但是我认为这不是最好的方法。

const o = {
  name: "unknow",
  key: "key"
  value: "value"
  id ": 12
}


我如何更新/覆盖对象的所有键(但ID除外)?

更新

两个对象具有相同的键。但是它们的密钥具有不同的价值。我需要更新第一个对象的所有键(不包括其ID)。

3 个答案:

答案 0 :(得分:1)

我怀疑您正在寻找类似assignBut的东西:它在ob上设置了oa的属性,但指定了以下属性:

const assignBut = (prop, oa, ob) => {
  for (let key of Object.keys(ob))
    // Check that I also verify that the property
    // to set should be part of "oa" object. This
    // prevents adding new properties: it just updates 
    // existing ones.
    if (key !== prop && oa.hasOwnProperty(key))
      oa[key] = ob[key]
}

const oa = {
  name: "unknow",
  key: "key",
  value: "value",
  id: 12
}

const ob = {
  name: "xxx",
  key: "yyy",
  value: "zzz",
  other: "aaa",
  yetAnother: 289,
  id: 15
}

assignBut('id', oa, ob)

console.log(oa)

另一种忽略给定属性的方法

一个人可能会利用解构和计算出的属性名称来忽略整个给定属性,因此for..of只需要检查ob中的每个属性是否存在于oa中即可进行设置

另外,可以保存检查以验证ob中存在oa属性,并执行oaob键的交集:

const oa = {
  name: "unknow",
  key: "key",
  value: "value",
  id: 12
}

const ob = {
  name: "xxx",
  key: "yyy",
  value: "zzz",
  other: "aaa",
  yetAnother: 289,
  id: 15
}

const intersect = (xs, ys) => xs.filter(x => ys.includes(x))

const assignBut = (prop, oa, {
  [prop]: omitted,
  ...ob
}) => {
  const sharedKeys = intersect(Object.keys(oa), Object.keys(ob))

  for (let key of sharedKeys)
    oa[key] = ob[key]
}

assignBut('id', oa, ob)

console.log(oa)

答案 1 :(得分:1)

您可以像下面这样遍历Object.keys-

const o = {
      name: "unknow",
      key: "key",
      value: "value",
      id : 12
    };
    
    Object.keys(o).forEach((key)=>{
      if(key !=="id"){
         console.log(o[key]) //value
      }
     }
    );

答案 2 :(得分:1)

以下方法基于lodash。如果您不习惯使用图书馆,请忽略。

omit的优点是您可以传递键数组并忽略多个键。

还有一个名为pick的函数,您只能在其中选择所需的某些属性。

_。忽略

const o = { name: "unknow", key: "key", value: "value", id: 12 }
const props = { name: "foo", key: "key2", value: "bar", id: 15 };

const final = _.assign({}, o, _.omit(props, 'id'));

console.log(final)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

_。pick

const o = { name: "unknow", key: "key", value: "value", id: 12 }
const props = { name: "foo", key: "key2", value: "bar", id: 15, test: 'abc', hello: 'world' };

const final = _.assign({}, o, _.pick(props, ['name', 'key', 'value']));

console.log(final)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

纯js实现

const o = { name: "unknow", key: "key", value: "value", id: 12 }
const propsOmit = { name: "foo", key: "key2", value: "bar", id: 15 };
const propsPick = { name: "foo", key: "key2", value: "bar", id: 15, test: 'abc', hello: 'world' };

const finalOmit = Object.assign({}, o, omit(propsOmit, 'id'));
const finalPick = Object.assign({}, o, omit(propsPick, ['id', 'test', 'hello']));

console.log(finalOmit)
console.log(finalPick)

function omit(obj, ignoreKeys) {
  if (!Array.isArray(ignoreKeys)) {
    ignoreKeys = [ ignoreKeys ];
  }
  
  const copy = Object.assign({}, obj);
  ignoreKeys.forEach((k) => delete copy[k]);
  return copy;
}

function pick(obj, selectKeys) {
  if (!Array.isArray(selectKeys)) {
    selectKeys = [ selectKeys ];
  }
  
  const copy = {};
  ignoreKeys.forEach((k) => copy[k] = obj[k]);
  return copy;
}

参考文献: