0

****I have an array containing other arrays which is as below, my purpose is to find the arrays in it which are containing the string Type 4 Sheets at their 3rd position inside them. Generally speaking, it will be really great if I can get only those arrays inside the bigger array which contains a specific string at nth position****

[[sheets at upper, intermediate and lower, Type 4 Sheets, Sheets, 8.0, 1.5, 5.0, , 3.0, Exact Sized Material # 1],
 [sheets at  back , Type 4 Sheets, Sheets, 8.0, 1.0, 2.5, , 1.0, Exact Sized Material # 2], 
[dfsdfdsfsd, Type 2 Gola, Gola, 5.0, 1.5, 2.5, , 2.0, Exact Sized Material # 3],
 [sheets at vertical divider, Type 1 Gola, Gola, , 1.5, 2.5, , 1.0, Exact Sized Material # 4], 
[sheets at shutters,  , , 2.0, , 2.5, , 4.0, Exact Sized Material # 5], 
[sheets at shutters,  , , 8.0, , , , 12.0, Exact Sized Material # 6], 
[sheets at shutters,  , , 8.0, , , , 8.0, Exact Sized Material # 7], 
[sheets at shutters,  , , 2.5, , , , 8.0, Exact Sized Material # 8], 
[sheets at shutters,  , , 1.5, , , , 8.0, Exact Sized Material # 9], 
[sheets at shutters,  , , 2.5, , , , 8.0, Exact Sized Material # 10], 
[sheets at shutters,  , , 2.5, , , , 4.0, Exact Sized Material # 11], 
[sheets at shutters,  , , 2.0, , , , 32.0, Exact Sized Material # 12], 
[sheets at shutters,  , , 2.5, , , , 32.0, Exact Sized Material # 13]]
1
  • Did you check forEach structures? Are you looking for something like array.forEach(function(a) { if (a[2] =="Type 4 Sheets") results.push(a) }); ? Results should have all arrays that contains the given string in the 3rd. position. Commented Sep 7, 2018 at 17:58

2 Answers 2

1

see if this helps

arr.filter(x => x[positionToCheck] === stringToCheck)

based on comments here is transpiled version

arr.filter(function (x) {
  return x[positionToCheck] === stringToCheck;
});
Sign up to request clarification or add additional context in comments.

3 Comments

You'll need to rewrite the arrow functions as standard JS 1.6, as Google Apps Script has only some support for newer JS features. (Array#filter is supported)
I tried var filtered = exactSizedMaterialsData.filter(x => x[3] === "Type 4 Sheets"); according to the provided solution but I got Syntax error for this line.
@ArsalanAhmadIshaq note that index 3 is position 4 within the array
0

This can be done in javascript by using forEach:

results = [];
outerarray.forEach(function(a) { 
  if (a[2]==="Type 4 Sheets") 
    results.push(a) 
});

a will be each array inside the outerarray

results should have all internal arrays that contains the given string in the 3rd. position.

Comments

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.