简体   繁体   English

返回一个新对象,其属性是给定对象中的那些属性,并且其键存在于给定数组中。

[英]Return a new object whose properties are those in the given object and whose keys are present in the given array.

:) I need to return a new object whose properties are those in the given object and whose keys are present in the given array. :)我需要返回一个新对象,该对象的属性是给定对象中的那些属性,并且其键在给定数组中存在。

code attempt: 代码尝试:

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

function select(arr, obj) {
 var result = {};
 var array = [];
 for (var key in obj){
   array.push(key);
 }
var num = arr.length + obj.length; 
    for (var i = 0; i < num; i++) {
     for(var j = 0; j < num; j++) {
       if (arr[i] === array[j]) {

       result.array[i] = obj.arr[i];
     }
     }
    }
    return result;
}

(incorrect) result: (不正确)结果:

{}

desired result: 预期结果:

// --> { a: 1, c: 3 }

Any advice? 有什么建议吗? Thank you! 谢谢! :) :)

You could iterate the given keys, test if it is a key in the object and assign the value to the same key in the result object. 您可以迭代给定的键,测试它是否是对象中的键,然后将值分配给结果对象中的相同键。

 function select(arr, obj) { var result = {}; arr.forEach(function (k) { if (k in obj) { result[k] = obj[k]; } }); return result; } var arr = ['a', 'c', 'e'], obj = { a: 1, b: 2, c: 3, d: 4 }; console.log(select(arr, obj)); 

Longer, but more readable version: 更长但更易读的版本:

 var arr = ['a', 'c', 'e'], obj = {a:1,b:2,c:3,d:4}, hash = {}; arr.forEach(function(v){ //iterate over each element from arr Object.keys(obj).some(function(c){ //check if any key from obj is equal to iterated element from arr if (v == c) { hash[v] = obj[c]; //if it is equal, make a new key inside hash obj and assign it's value from obj to it } }); }); console.log(hash); 

Short version: 简洁版本:

 var arr = ['a', 'c', 'e'], obj = {a:1,b:2,c:3,d:4}, hash = {}; arr.forEach(v => Object.keys(obj).some(c => v == c ? hash[v] = obj[c] : null)); console.log(hash); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个整数数组,返回正数,其中存在等价的负数 - Given an array of integers return positives, whose equivalent negatives present in it 编写一个函数,删除给定对象上的任何属性,其值是长于给定数字的字符串,并返回该对象 - Write a function that removes any properties on the given object whose values are strings longer than the given number and returns the object 如何返回一个数组,其中包含 object 中的前 2 个元素,其值属性是一个数组 - How to return an array with the first 2 elements in an object whose value properties are an array Lodash:返回其值(即Array)中有给定元素(即字符串)的对象的第一个键 - Lodash : return first key of object whose value(i.e Array) has a given element (i.e string) in it 找到键值接近给定值的对象的有效方法 - Effiecient way to find the object whose key value is close to a given value 如何在给定一组键的情况下创建嵌套对象 - How to create a nested object given an array of keys 删除值大于给定数字的任何属性 - Remove any properties whose values are numbers greater than the given number 从对象中选取给定属性的值到数组中 - Pick values of given properties from object into array 如何使用 Joi 验证嵌套 object 的键应与外部对象匹配的另一个键的值为数组的键? - How to validate nested object whose keys should match with outer objects another key whose value is array using Joi? 使用给定值搜索和删除的最佳方法,用于从 object 内的数组中删除并返回 object javascript 的新数组? - best way to search and delete with given values, for delete from array inside the object and return new array of object javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM