0

I am trying to do a forEach loop and take each item and split it at a character. In this case I want to split the array item text at "||". What I currently have is:

var array = [
  [
    "featured", ["feat||1", "feat||2", "feat||3"]
  ],
  [
    "new", ["new||1", "new||2", "new||3"]
  ]
];

var taxIDs = [];


array.forEach((i) => {
  if (i[1].length) {
    taxIDs.push(i[1].split("||")[1]);
  }
});

So I was trying to take i[1] and splitting it at "||" and getting the second item in that split array, but doing this throws and error and not sure the best approach to accomplish what I need?

EDIT (this is what I see when console logging my array:

(3) [Array(2), Array(2)]
 0: (2) ["featured", Array(3)]
   0: "featured"
   1: (3) ["feat||1", "feat||2", "feat||3"]
 1: (2) ["news", Array(2)]
   0: "news"
   1: (2) ["news||1", "news||2", "news||3"]

So there are 3 levels of arrays in this array.

8
  • 3
    Shouldn't those array values be strings? You can't split on non-string values. Commented Apr 19, 2019 at 16:04
  • 3
    The definition of that array is already an error. Are those values supposed to be strings? Commented Apr 19, 2019 at 16:05
  • and you seem to be missing some commas in that array. Commented Apr 19, 2019 at 16:07
  • What exactly are the values you need? And where? Commented Apr 19, 2019 at 16:10
  • @GetOffMyLawn correct those values should be strings and have edited my post to reflect that. Commented Apr 19, 2019 at 16:23

3 Answers 3

1

Here you have one possible approach using Array.reduce() and destructuring of the nested arrays.

var array = [
  [
    "featured", ["feat||1", "feat||2", "feat||3"]
  ],
  [
    "new", ["new||1", "new||2", "new||3"]
  ]
];

var taxIDs = array.reduce(
    (acc, [str, arr]) => [...acc, arr.map(str => +str.split("||")[1])], 
    []
);

console.log("taxIDs is: ", taxIDs);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

You can also replace the .map() by this one that uses match() on the string:

arr.map(str => +str.match(/\d+/)[0])

Also, you can avoid the unnary plus if you want the ids to be keep as strings:

arr.map(str => str.split("||")[1])
// OR
arr.map(str => str.match(/\d+/)[0])
Sign up to request clarification or add additional context in comments.

2 Comments

This is so close to what I am looking for. I just tested this and it does take the values splits them at "||" and then takes the number values and pushes them to the taxIDs array. Is it possible to instead of pushing each number to taxIDs and making each number its own array item, to push each array to taxIDs as an array, so instead of taxIDs = [1,2,3,1,2,3] make it: taxIDs = [ [1,2,3], [1,2,3] ]?
@user9664977 Sure, just remove the spread syntax before the map, check the updated snippet.
0

var array = [
  [
    "featured", ["feat||1", "feat||2", "feat||3"]
  ],
  [
    "new", ["new||1", "new||2", "new||3"]
  ]
];

var taxIDs = [];

array.forEach(item => {
  taxIDs.push(item[1].map(elem => elem.split('||')[1]));
});

console.log(taxIDs);

2 Comments

I don't need them in an object like that. It looks like you are getting both values from the child array. I only need the second item. So instead of needing both ["featured", [ feat||1, "feat||2", "feat||3"] ] I only need the values of the second item in that array which is ["feat||1", "feat||2, "feat||3]. I then want to split each item in that array at "||" and only push the number to the taxIDs array. So splitting it at "||" and pushing the numbers to taxIDs, taxIDs should look like [1,2,3].
@user9664977 Yes sure, just replace taxIDs with an array and push the result of the map function, check my updated answer
0
var array = [
  [
    "featured", ["feat||1", "feat||2", "feat||3"]
  ],
  [
    "new", ["new||1", "new||2", "new||3"]
  ]
];

var taxIDs = [];

array.forEach(function(value, index){ value[1].forEach(function(value, index){taxIDs.push(value.split("||")[0])})})

taxIDs will look like this

 ["feat", "feat", "feat", "new", "new", "new"]

or if you want keys like

taxIDs
(6) ["1", "2", "3", "1", "2", "3"]

then little bit modifications in loop as following

array.forEach(function(value, index){ value[1].forEach(function(value, index){taxIDs.push(value.split("||")[1])})})

This will work for you now, according to your updated Array Structure and question

this forEach loop will give you basic knowledge, how I my logic works

3 Comments

My array doesn't look like that when I console log it out. My array has arrays inside arrays. So in my above question it looks like that with 3 levels of arrays. The 1 level being the main array, inside that there are arrays as the main arrays children. And inside each child array there is two items a string as the first item and another array as the second item. This 'grandchild' array then has string items inside that. I made an edit to my original question showing what my array looks like when console logged.
okay then try this array.forEach(function(value, index){ value[1].forEach(function(value, index){taxIDs.push(value.split("||")[0])})})
your taxIDs will have ` ["feat", "feat", "feat", "new", "new", "new"]`

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.