0

Currently I have an each loop cycling through some parsed JSON objects (arrays)

But I'm not achieving the desired effect that I'm going for with the tagz variable. They aren't combining into one array. There is about 15 different arrays full of keywords and they aren't combining into one.

How would I go about concat'ing multiple unique arrays into one that's in an each loop?

$.each(i.c.u, function(i,img){
    var tags = [img.keywords];

   var tagz = keywords.concat(keywords);
Andrew Squire
  • 145
  • 1
  • 1
  • 17
  • Can you include input array and expected result at Question? – guest271314 Feb 25 '17 at 02:34
  • Is `img.keywords` already an array? If so, you just turned `[1,2,3]` into `[[1,2,3]]` so when you concat `[[4,5,6]]` it'll turn into `[[1,2,3],[4,5,6]]` – Sterling Archer Feb 25 '17 at 02:36
  • 2
    Possible duplicate of [Merge/flatten an array of arrays in JavaScript?](http://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays-in-javascript) – 4castle Feb 25 '17 at 02:36
  • 1
    Suggest adding the `jquery` tag, since `$.each` isn't standard JavaScript. – TheJim01 Feb 25 '17 at 02:44
  • *keywords* appears to be a property of *img*, not a local variable. You probably want `tagz = tagz.concat(img.keywords)`. – RobG Feb 25 '17 at 02:48
  • img.keywords is formatted for one returned array from multiple arrays that the each loop is going through @sterling archer. I tried the suggested approach --> 4castle with var tagz = [].concat.apply([], tags); and it isn't changing the fact that I still get seperate arrays that aren't being joined into one for var tagz – Andrew Squire Feb 25 '17 at 02:50
  • 3
    You need to show an example of *img* and what you expect the outcome to be, otherwise everyone is just guessing. Consider using `Array.prototype.forEach` instead of jQuery's similar but different `each`. – RobG Feb 25 '17 at 02:51
  • Array[1]0: "key1 key2 key3 key4 key5 key6 key7 key8 key9"length: 1__proto__: Array[0] pf.js:77 Object {blah: "233020", blah: "132323", blah: "23989328", blah: "2390239", blah: 3…} Array[2]0: "key10 key11 key12 key13 key14 key15 key16 key17 key18"length: 1__proto__: Array[0] pf.js:77 Object {blah: "233020", blah: "132323", blah: "23989328", blah: "2390239", blah: 3…} Straight from the console is the output of tagz for the arrays, I'd like them to be joined into tagz so they aren't seperate. There's more than those arrays but you get the picture – Andrew Squire Feb 25 '17 at 03:01
  • 1
    Use `JSON.stringify(tagz)` at `console`, include result at Question. – guest271314 Feb 25 '17 at 03:02
  • I'll just give the raw result of JSON.stringify(tagz) : ["tomorrowland festival belgium europe music concert costume carnival travel"], still not combining them. Gonna try Array.prototype.forEach next. – Andrew Squire Feb 25 '17 at 03:17
  • 1
    `["tomorrowland festival belgium europe music concert costume carnival travel"]` is a single string at index `0` of an array. – guest271314 Feb 25 '17 at 03:22
  • `var res = tagz[0].split(" ") // ["tomorrowland", "festival", "belgium", "europe", "music", "concert", "costume", "carnival", "travel"]` to get an array of words in string at index `0` of `tagz` array. What do you mean by "not combining them"? What is expected result? – guest271314 Feb 25 '17 at 03:26

1 Answers1

0

set this array method and use.

code

Array.prototype.concatAll = function() {
  let result = [];
  this.forEach((array) => {
    result.push.apply(result, array);
  });
  return result;
};

var tagz = tags.concatAll();
Rach Chen
  • 1,322
  • 2
  • 10
  • 26