Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何基于其他数组中的特定动态键过滤/减少对象数组_Javascript_Arrays_Ecmascript 6_Filter_Reduce - Fatal编程技术网

Javascript 如何基于其他数组中的特定动态键过滤/减少对象数组

Javascript 如何基于其他数组中的特定动态键过滤/减少对象数组,javascript,arrays,ecmascript-6,filter,reduce,Javascript,Arrays,Ecmascript 6,Filter,Reduce,我要解释我的问题。我没有时间多考虑这个问题,我有一个拦截器,我想不出来,所以任何帮助都会很感激。例如,我有一个对象数组,它可以是包含100多个元素的数组: const arr = [ { value: '1', id: 1, number: 1, other: 'example', data: '6' }, { value: '2', id: 2, number: 2, other: 'example', data: '7' }, { value: '3', id:

我要解释我的问题。我没有时间多考虑这个问题,我有一个拦截器,我想不出来,所以任何帮助都会很感激。例如,我有一个对象数组,它可以是包含100多个元素的数组:

  const arr = [
    { value: '1', id: 1, number: 1, other: 'example', data: '6' },
    { value: '2', id: 2, number: 2, other: 'example', data: '7' },
    { value: '3', id: 3, number: 3, other: 'example', data: '8' },
    { value: '4', id: 4, number: 4, other: 'example', data: '9' },
    { value: '5', id: 5, number: 4, other: 'example', data: '10' },
  ];
在另一个数组中,我有字符串,其中包含如下特定键:

  const keys = ['value', 'id', 'number'];
 const arr = [
    { value: '1', id: 1, number: 1 },
    { value: '2', id: 2, number: 2 },
    { value: '3', id: 3, number: 3 },
    { value: '4', id: 4, number: 4 },
    { value: '5', id: 5, number: 4 },
  ];
我的问题是,我想返回变量arr,它只包含基于keys变量值的对象。诸如此类:

  const keys = ['value', 'id', 'number'];
 const arr = [
    { value: '1', id: 1, number: 1 },
    { value: '2', id: 2, number: 2 },
    { value: '3', id: 3, number: 3 },
    { value: '4', id: 4, number: 4 },
    { value: '5', id: 5, number: 4 },
  ];
我希望动态使用它,因为keys变量中的值不是常量,可以是值、其他值、数据或仅数据或id等。

您可以使用Array.prototype.map和reduce来实现

常数arr=[ {value:'1',id:1,number:1,other:'example',data:'6'}, {value:'2',id:2,number:2,other:'example',data:'7'}, {value:'3',id:3,number:3,other:'example',data:'8'}, {value:'4',id:4,number:4,other:'example',data:'9'}, {value:'5',id:5,number:4,other:'example',data:'10'}, ]; 常量键=['value','id','number']; const res=arr.mapitem=>keys.reduceacc,key=>{…acc[key]:item[key]},{}; console.logres; .作为控制台包装器{min height:100%!important;top:0;}您可以使用Array.prototype.map和reduce来完成

常数arr=[ {value:'1',id:1,number:1,other:'example',data:'6'}, {value:'2',id:2,number:2,other:'example',data:'7'}, {value:'3',id:3,number:3,other:'example',data:'8'}, {value:'4',id:4,number:4,other:'example',data:'9'}, {value:'5',id:5,number:4,other:'example',data:'10'}, ]; 常量键=['value','id','number']; const res=arr.mapitem=>keys.reduceacc,key=>{…acc[key]:item[key]},{}; console.logres;
.as console wrapper{min height:100%!important;top:0;}创建一个函数,该函数将基于keys数组返回,并使用该函数映射

const arr = [
        { value: '1', id: 1, number: 1, other: 'example', data: '6' },
        { value: '2', id: 2, number: 2, other: 'example', data: '7' },
        { value: '3', id: 3, number: 3, other: 'example', data: '8' },
        { value: '4', id: 4, number: 4, other: 'example', data: '9' },
        { value: '5', id: 5, number: 4, other: 'example', data: '10' },
      ];

    const keys = ['value', 'id', 'number'];

    function pick(obj, keys){
        let result = {};
        for(let i=0; i<keys.length; i++){
            result[keys[i]] = obj[keys[i]];
        }
        return result;
    }

    let finalArr = arr.map( value => pick(value,keys));

    console.log(finalArr);

创建一个基于键数组返回的函数,并使用该函数映射

const arr = [
        { value: '1', id: 1, number: 1, other: 'example', data: '6' },
        { value: '2', id: 2, number: 2, other: 'example', data: '7' },
        { value: '3', id: 3, number: 3, other: 'example', data: '8' },
        { value: '4', id: 4, number: 4, other: 'example', data: '9' },
        { value: '5', id: 5, number: 4, other: 'example', data: '10' },
      ];

    const keys = ['value', 'id', 'number'];

    function pick(obj, keys){
        let result = {};
        for(let i=0; i<keys.length; i++){
            result[keys[i]] = obj[keys[i]];
        }
        return result;
    }

    let finalArr = arr.map( value => pick(value,keys));

    console.log(finalArr);

请张贴一份你的尝试,并具体说明你被困的地方。人们会很乐意提供帮助。迭代数组是给定的,因此问题是从对象中删除不需要的键。您可以创建一个新对象并通过迭代关键点来设置其道具,也可以使用delete从现有对象中删除它们。前者意味着您将要使用arr.map,而后者是arr.foreach的工作。请发布您的尝试,并具体说明您遇到的困难。人们会很乐意提供帮助。迭代数组是给定的,因此问题是从对象中删除不需要的键。您可以创建一个新对象并通过迭代关键点来设置其道具,也可以使用delete从现有对象中删除它们。前者意味着您需要使用arr.map,而后者是arr.foreach的工作这正是我所需要的。非常感谢你;抱歉,我没有检查它是否在数组中创建了更多对象,并且我需要一个对象中的所有内容。我不得不改变对答案的接受,但你的答案仍然是好的;再次非常感谢。很抱歉你的失礼。我已经更新了我的代码。再次感谢,我喜欢减少+map而不是for循环,所以我再次更改它:谢谢!!你是对的。如果你不介意的话,我给了他最好的答案。这正是我需要的。非常感谢你;抱歉,我没有检查它是否在数组中创建了更多对象,并且我需要一个对象中的所有内容。我不得不改变对答案的接受,但你的答案仍然是好的;再次非常感谢。很抱歉你的失礼。我已经更新了我的代码。再次感谢,我喜欢减少+map而不是for循环,所以我再次更改它:谢谢!!你是对的。如果你不介意的话,我给了他最好的答案。