-1

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

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


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);
terik_poe
  • 523
  • 4
  • 17
  • hey @Rajesh it's a problem bro! and i want to know how to filter the nested data and append to another datatype `[]` in an efficient way – terik_poe Feb 01 '17 at 06:32
  • @Rajesh http://stackoverflow.com/questions/41950644/getting-array-from-object-in-js/41950681#41950681 Then, what's this bro? – terik_poe Feb 01 '17 at 06:35
  • You want to filter data using what key? – ibrahim mahrir Feb 01 '17 at 06:38
  • using lodash but i dont know which lodash methods to use, – terik_poe Feb 01 '17 at 06:42
  • @ibrahimmahrir it would be nice if you provide some hints or solution bro – terik_poe Feb 01 '17 at 06:43
  • @Rajesh Bro `console.log('Data filteration', data); const filteredData = []; filteredData.push(data[0][0]); data[1].forEach((i) => { filteredData[0].push(i.jobId); }); console.log('filteredData', filteredData);` here's the code i tried, but i think it's not an efficient way to these types of calculation, i think it's a static way, i want dynamic ways to handle the data – terik_poe Feb 01 '17 at 06:45
  • @terik_poe can't post a solution if I don't fully understand what you want. So is what you want is to return an array of only desired items selected by a key or some other criteria? Or what you want is to just unwrap the objects out of that array (you want to get rid of the array that only contains one object)? – ibrahim mahrir Feb 01 '17 at 06:48
  • @terik_poe if I understand right, `data[0][0]` refers to user data. What does `data[0][1]` refers to? – Rajesh Feb 01 '17 at 07:29
  • `data[0][1]` refers to list of array in mine case there's `1` `object` and every `object` have jobId which is also `object`, which i want to append it into new array with 1 `object` and put all the `jobId` in a `[]` inside the main new `[]` first `object` – terik_poe Feb 01 '17 at 07:37
  • please see mine REQUIRED DATA – terik_poe Feb 01 '17 at 07:38

1 Answers1

2

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. 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. Note, this will yield an array of same size.
  • Array.filter on the other hand is use to filter array based on condition. 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. You can achieve same by chaining .filter().map() but then its an overkill as it would result in O(2n).
  • Object.assign In JS objects are passed by reference. 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. 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?

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. 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)

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • but there's no jobId array in the object – terik_poe Feb 01 '17 at 07:19
  • @terik_poe Please check update. Also apologies for misunderstanding – Rajesh Feb 01 '17 at 07:43
  • why there is still a `-1` bro for the QUESTION – terik_poe Feb 01 '17 at 07:47
  • @terik_poe I'm not the downvoter but you should learn from this case that sharing effort is a very important. That shows you have tried and got stuck somewhere. – Rajesh Feb 01 '17 at 07:49
  • i tried to solve the problem bro sorry for not providing the effort codes, as i am new and haven't worked with nested `[]` methods, it would be pleasure if you can explain me what does `reduce` `filter` and `assign` methods do. – terik_poe Feb 01 '17 at 07:55
  • @terik_poe Please check the update. Advice you to refer links for more information – Rajesh Feb 01 '17 at 08:11