简体   繁体   English

数组过滤和数据提取并追加到新数组

[英]Array filteration and Extraction of data and append to new Array

I have an array with nested array 我有一个带有嵌套数组的数组

I want the data to append in a new array. 我希望数据附加到新数组中。

For the data extraction or filtration what method's i have to use, using library such as lodash 对于数据提取或过滤,我必须使用诸如lodash库使用什么方法

DATA 数据

[ 
    [ 
        {
            _id: 588d9b8a608f2a66c298849f,
            email: 'sd@',
            password: '$2a$10$6..L3c3tANi6ydt9gZbc1O6prPfUd3RB.ner5lilxRyEwo1lPsSoC',
            isJobSeeker: true,
            __v: 0,
            lastName: 'shrestha',
            firstName: 'manish',
            isSeeker: true 
        }
    ],
    [ 
        { 
            _id: 588dbb4f7a48ce0d26cb99fd,
            jobId: [Object],
            seekerId: 588d9b8a608f2a66c298849f,
            employerId: 588d7d6c0ec4512feb819825,
            __v: 0,
        }
    ]
]

REQUIRED DATA 所需数据

[
    { 
        _id: 588d9b8a608f2a66c298849f,
        email: 'sd@',
        password: '$2a$10$6..L3c3tANi6ydt9gZbc1O6prPfUd3RB.ner5lilxRyEwo1lPsSoC',
        isJobSeeker: true,
        __v: 0,
        lastName: 'shrestha',
        firstName: 'manish',
        isSeeker: true 
    },
    jobId: [{}, {}, {}] // ARRAY WITH OBJECTS
]

also i want to change the jobId key to other key of custom string as jobs 我也想将jobId键更改为自定义string其他键作为jobs


Following is my attempt: 以下是我的尝试:

console.log('Data filteration', data);
const filteredData = [];
filteredData.push(data[0][0]);
data[1].forEach((i) => {
  filteredData[0].jobs = i.jobId
});
console.log('filteredData', filteredData);

First you should clean you data to have a better structure. 首先,您应该清理数据以拥有更好的结构。

[
  [
    { ... }
  ],
  [
    { ... }
  ]
]

In this datastructure, its difficult to understand what does inner arrays signify. 在此数据结构中,很难理解内部数组的含义。 Instead you should use an object . 相反,您应该使用一个object That would define the purpose of array and make your code more readable. 这将定义数组的用途,并使您的代码更具可读性。

 var data=[[{_id:"588d9b8a608f2a66c298849f",email:"sd@",password:"$2a$10$6..L3c3tANi6ydt9gZbc1O6prPfUd3RB.ner5lilxRyEwo1lPsSoC",isJobSeeker:!0,__v:0,lastName:"shrestha",firstName:"manish",isSeeker:!0}],[{_id:"588dbb4f7a48ce0d26cb99fd",jobId:["test","test1"],seekerId:"588d9b8a608f2a66c298849f",employerId:"588d7d6c0ec4512feb819825",__v:0}]]; var cleanedData = { userData: data[0], userJobMap: data[1], } var result = cleanedData.userData.reduce(function(p,c){ if(c.isJobSeeker){ var job = cleanedData.userJobMap.filter(x=> x.seekerId === c._id); // To copy object and not reference var t = Object.assign({}, c, { jobId: job[0].jobId }); p.push(t) } return p }, []) console.log(result) 

References 参考文献

  • Array.map is a tool that iterates over all elements and return different value say a single property of return double value of all numbers in array . Array.map是一个对所有元素进行迭代并返回不同值的工具,例如, 将array中所有数字的double值返回单个属性。 Note, this will yield an array of same size. 注意,这将产生相同大小的数组。
  • Array.filter on the other hand is use to filter array based on condition. 另一方面, Array.filter用于根据条件过滤数组。 This will return a subset of original data but elements will be same. 这将返回原始数据的子集,但元素将相同。 You cannot change element structure. 您不能更改元素结构。
  • Array.reduce is a tool that address cases where you need to return selected elements with parsed value. Array.reduce是一种工具,用于解决需要返回具有解析值的选定元素的情况。 You can achieve same by chaining .filter().map() but then its an overkill as it would result in O(2n) . 您可以通过链接.filter().map()来实现相同目的,但是这样做会产生过大的杀伤力,因为这会导致O(2n)
  • Object.assign In JS objects are passed by reference. Object.assign在JS对象中通过引用传递。 So if you assign an object to a variable, you are not copying entire object, but only reference. 因此,如果将对象分配给变量,则不是复制整个对象,而只是复制引用。 So it you change anything in this variable, it will also reflect in original object. 因此,您可以更改此变量中的任何内容,它也会反映在原始对象中。 To avoid this, you need to copy value. 为避免这种情况,您需要复制价值。 This is where Object.assign comes. 这是Object.assign来源。 Note, its not supported by old browsers. 请注意,旧浏览器不支持它。 For them you can check following post - What is the most efficient way to deep clone an object in JavaScript? 对于他们,您可以查看以下文章- 在JavaScript中深度克隆对象的最有效方法是什么?

Note: All array functions are part of functional programming paradigm and are used to make your code more readable and concise but they come at an expense of performance. 注意:所有数组函数都是函数编程范例的一部分,用于使您的代码更具可读性和简洁性,但是它们以牺牲性能为代价。 Traditional for will always perform faster then them. 传统的for总是比它们更快地执行。 So if you want to focus on performance, always try to use for ( though difference is very small but can add up for multiple cases and become substantial ) 所以,如果你想专注于表演,总是想用for虽然差别是很小的,但是,最多可以添加多个病例,并成为实质性的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM