[英]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)
.filter().map()
but then its an overkill as it would result in O(2n)
. 您可以通过链接.filter().map()
来实现相同目的,但是这样做会产生过大的杀伤力,因为这会导致O(2n)
。 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.