1

I have an array of arrays that gets returned from a mongo query. I want to take and remove the first item/element in each array. Here is my node route

Route partial

let queries = [
    User.find({"companyID":req.user.companyID}, (err, foundUsers) => {
        if (err) throw err;
    }),
];
Promise.all(queries)
.then(results => {
    res.render('hr/employees', {header: 'EMPLOYEES', users: results[0]});
}).catch( err => {
    req.flash('error', err);
    res.render('hr/employees', {header: 'EMPLOYEES'});
});

Sample arrays

What I have now

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

What I want

[[2, 3], [5,6], [8,9]]
3
  • Please post your array of arrays. Also from that array of arrays write your expected array of arrays. Commented Sep 12, 2018 at 1:50
  • @VicJordan That is pointless. I will add a sample set of arrays but I doubt anyone wants to look at 2k lines of JSON which is what my arrays will hold. Commented Sep 12, 2018 at 2:00
  • no one is asking you to post actual 2k lines of json but expect you to post sample structure with lesser lines of code. Hope you understand Commented Sep 12, 2018 at 2:08

4 Answers 4

1

Let's suppose results is an array of arrays. Use slice inside a map to remove the first element and return the desired array.

Promise
  .all(queries)
  .then(results => (results.map(result => (result.slice(1)))))
  .then(adaptedResults => res.render('hr/employees', { header: 'EMPLOYEES', users: adaptedResults[0] });

EDIT: Using the values you have provided.

console.log([[1, 2, 3], [4, 5, 6], [7, 8, 9]].map(arr => (arr.slice(1))))

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, when I plug that into the route it give syntax errors and I don't know .map well enough to troubleshoot. How would I take what you have and then send that result to my res.render?
Thanks for your edit it worked! I had to add a few more ) but otherwise perfect thank you.
I've updated my answer. Also, you could do it inside the same .then as follows: .then(results => { const adaptedResults = results.map(result => (result.slice(1))); res.render('hr/employees', {header: 'EMPLOYEES', users: adaptedResults[0]}); })
1

Something like this should do the job,

const arrOfArrays = []; // get value from response
// remove first element from all arrays safely
const newArr = arrOfArrays.map(arr => (arr && arr.length) ? arr.shift() : arr); 

Edit: As @rghossi pointed out, the above snippet should return an array of first elements. So the right way to do it would be,

const arrOfArrays = []; // get value from response
// remove first element from all arrays safely
const newArr = arrOfArrays.map(arr => (arr && arr.length) ? arr.slice(1) : arr); 

4 Comments

Just gave that a try and tried with a few modifications to it and neither seemed to work. Maybe I will have to try dealing with this issue inside EJS
@joshk132 I just updated my answer. .map will return a new array and you will have to assign it to a new variable to use the updated results.
@joshk132 Glad to hear that. If you could accept the answer, it'd help others.
That's actually incorrect. arr.shift() will return only the removed element, not the modified array. You could use slice(1) instead or change your implementation to return arr after arr.shift().
0

Sounds like Array.prototype.shift() is what you're looking for: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

1 Comment

Yes sorta, I know of shift() but that will just remove the first array in my set of arrays.
0

You can use slice() and shift() method to that easily

for (i = 0; i < results.length; i++) {
    results[i].slice(1);//OR you can use results[i].shift()
}

Or you can modify your code like:

Promise.all(queries)
.then(results => (results.map(result => (result.shift())) {
    //OR you can use result.slice()
    //Your code here
});

It will remove all first element in an array of arrays.

1 Comment

I realize that is an option but was looking for a cleaner way of doing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.