从现有数组和对象创建新对象

时间:2022-11-26 20:14:06

I'm trying to return a new object with properties given in the existing object and keys present in a given array. I can't mutate the object and if the keys are present in the array but not in the object the key should be ignored. I get hung up on how to compare the array elements to the objects keys.

我正在尝试返回一个新对象,其中包含现有对象中给出的属性以及给定数组中存在的键。我无法改变对象,如果键存在于数组中但不存在于对象中,则应忽略该键。我想知道如何将数组元素与对象键进行比较。

function picker(array, obj) {
    var newObj = {};
    for (var i = 0; i < arrary.length; i++) {
        if (array[i] !== obj[i]) {
            newObj[array[i]] = obj[i];
        }
    }
    return newObj;
}

var array = [
    'a',
    'c',
    'e'
];
var obj = {
    a: 1,
    b: 2,
    c: 3,
    d: 4
};

var bubble = picker(array, obj);
console.log(bubble);   result --> `{ a: 1, c: 3 }`

3 个解决方案

#1


1  

You probably meant something like this:

你可能意味着这样的事情:

function picker(array, obj) {
  var newObj = {};
  for (var i = 0; i < array.length; i++) {
    if (array[i] in obj) {
      newObj[array[i]] = obj[array[i]];
    }
  }
  return newObj;
}
var array = ['a', 'c', 'e'];
var obj = {a: 1, b: 2, c: 3, d: 4};
console.log(picker(array, obj));

That is, if obj contains the property with name array[i], then add that property to newObj.

也就是说,如果obj包含名为array [i]的属性,则将该属性添加到newObj。

Maybe it will be clearer if you iterate with reduce, forEach or for...of, then you won't be confused by the index i.

如果你用reduce,forEach或for ...迭代会更清楚,那么你就不会被索引i搞糊涂了。

function picker(array, obj) {
  return array.reduce(function(newObj, key) {
    if (key in obj) newObj[key] = obj[key];
    return newObj;
  }, {});
}
var array = ['a', 'c', 'e'];
var obj = {a: 1, b: 2, c: 3, d: 4};
console.log(picker(array, obj));

#2


1  

You could do something like

你可以做点什么

const x = array.reduce((total, current) => current in obj ? ({
  ...total,
  [current]: obj[current]
}) : total, {});

Which basically creates an object, key after key, based only on items of the array, where the value per key is taken from obj. Check out the fiddle.

这基本上只创建一个对象,密钥后密钥,仅基于数组的项目,其中每个密钥的值取自obj。看看小提琴。

#3


0  

You haven't checked whether the key is existing in the array.

您尚未检查该键是否存在于数组中。

The condition if (array[i] !== obj[i]) { is trying to check the array key with object value instead of object key. Also obj[i] is incorrect, since obj[0] does not exist with the key 0.

条件if(array [i]!== obj [i]){试图用对象值而不是对象键检查数组键。 obj [i]也是不正确的,因为密钥0不存在obj [0]。

This is another variation using Object.keys instead of for loop.

这是使用Object.keys而不是for循环的另一种变体。

function picker(array, obj) {
  var newObj = {};
  Object.keys(obj).forEach(function(key){
    if(array.indexOf(key) > -1) {
      newObj[key] = obj[key];
    }
  });
  
  return newObj;
}
var array = ['a', 'c', 'e'];
var obj = {a: 1, b: 2, c: 3, d: 4};
console.log(picker(array, obj));

#1


1  

You probably meant something like this:

你可能意味着这样的事情:

function picker(array, obj) {
  var newObj = {};
  for (var i = 0; i < array.length; i++) {
    if (array[i] in obj) {
      newObj[array[i]] = obj[array[i]];
    }
  }
  return newObj;
}
var array = ['a', 'c', 'e'];
var obj = {a: 1, b: 2, c: 3, d: 4};
console.log(picker(array, obj));

That is, if obj contains the property with name array[i], then add that property to newObj.

也就是说,如果obj包含名为array [i]的属性,则将该属性添加到newObj。

Maybe it will be clearer if you iterate with reduce, forEach or for...of, then you won't be confused by the index i.

如果你用reduce,forEach或for ...迭代会更清楚,那么你就不会被索引i搞糊涂了。

function picker(array, obj) {
  return array.reduce(function(newObj, key) {
    if (key in obj) newObj[key] = obj[key];
    return newObj;
  }, {});
}
var array = ['a', 'c', 'e'];
var obj = {a: 1, b: 2, c: 3, d: 4};
console.log(picker(array, obj));

#2


1  

You could do something like

你可以做点什么

const x = array.reduce((total, current) => current in obj ? ({
  ...total,
  [current]: obj[current]
}) : total, {});

Which basically creates an object, key after key, based only on items of the array, where the value per key is taken from obj. Check out the fiddle.

这基本上只创建一个对象,密钥后密钥,仅基于数组的项目,其中每个密钥的值取自obj。看看小提琴。

#3


0  

You haven't checked whether the key is existing in the array.

您尚未检查该键是否存在于数组中。

The condition if (array[i] !== obj[i]) { is trying to check the array key with object value instead of object key. Also obj[i] is incorrect, since obj[0] does not exist with the key 0.

条件if(array [i]!== obj [i]){试图用对象值而不是对象键检查数组键。 obj [i]也是不正确的,因为密钥0不存在obj [0]。

This is another variation using Object.keys instead of for loop.

这是使用Object.keys而不是for循环的另一种变体。

function picker(array, obj) {
  var newObj = {};
  Object.keys(obj).forEach(function(key){
    if(array.indexOf(key) > -1) {
      newObj[key] = obj[key];
    }
  });
  
  return newObj;
}
var array = ['a', 'c', 'e'];
var obj = {a: 1, b: 2, c: 3, d: 4};
console.log(picker(array, obj));