最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to split array based on the element in another array - Stack Overflow

programmeradmin0浏览0评论

I have two array: array1 and array2. So array1 will be splitted based on the element insided array2. For example:

array1["1","2","3","4","5","6"]
array2["2","5"]

My code:

var prev = 0;
newArray = []; 
for (var ii in array2) {
    var index = array1.indexOf(array2[ii]);

    if (index != prev) {
        newArray.push(array1.slice(prev, index));
        prev = index;
    }
 }
 newArray.push(array1.slice(prev));

The result will be :

["1"],["2","3","4"],["5","6"]

But now i facing the problem of array1's element can be not in order. For example:["1","5","3","4","2","6"]. So based on the code i have, it will split the array1 wrongly because first element in array2 is "2", so it already split the array1 into two ["1","5","3","4"],["2","6"]. And next when e to "5", it cannot find it.

The expected result is:["1"],["5","3","4"],["2","6"]

So how to split array1 based on array2 no matter array1 in ascending ,descending or random order. Sorry my english is not good. Hope you guys can understand.

I have two array: array1 and array2. So array1 will be splitted based on the element insided array2. For example:

array1["1","2","3","4","5","6"]
array2["2","5"]

My code:

var prev = 0;
newArray = []; 
for (var ii in array2) {
    var index = array1.indexOf(array2[ii]);

    if (index != prev) {
        newArray.push(array1.slice(prev, index));
        prev = index;
    }
 }
 newArray.push(array1.slice(prev));

The result will be :

["1"],["2","3","4"],["5","6"]

But now i facing the problem of array1's element can be not in order. For example:["1","5","3","4","2","6"]. So based on the code i have, it will split the array1 wrongly because first element in array2 is "2", so it already split the array1 into two ["1","5","3","4"],["2","6"]. And next when e to "5", it cannot find it.

The expected result is:["1"],["5","3","4"],["2","6"]

So how to split array1 based on array2 no matter array1 in ascending ,descending or random order. Sorry my english is not good. Hope you guys can understand.

Share Improve this question edited Apr 2, 2018 at 3:52 aswzen 1,64234 silver badges48 bronze badges asked Apr 2, 2018 at 1:57 XionXion 4942 gold badges9 silver badges31 bronze badges 5
  • Are the values in array2 unique? – Arman Charan Commented Apr 2, 2018 at 2:01
  • @ArmanCharan Yes.. – Xion Commented Apr 2, 2018 at 2:02
  • If there are multiple "2"s in array1: should they all be split points? Or just the first occurrence? – Arman Charan Commented Apr 2, 2018 at 2:03
  • @ArmanCharan the values inside array1 also unique – Xion Commented Apr 2, 2018 at 2:04
  • Without it being in sorted order, the time plexity will grow substantially – vol7ron Commented Apr 2, 2018 at 2:27
Add a ment  | 

2 Answers 2

Reset to default 9

See Set and Array.prototype.reduce() for more info.

// Split Up.
const splitup = (array, keys) => (set => array.reduce((output, value) => {
  if (set.has(value)) output.push([value]) // Split.
  else output[output.length-1].push(value) // Append.
  return output
}, [[]]))(new Set(keys))

// Output.
const output1 = splitup(["1","2","3","4","5","6"], ["2","5"])
console.log(...output1) // ["1"],["2","3","4"],["5","6"]
const output2 = splitup(["1","5","3","4","2","6"], ["2","5"])
console.log(...output2) // ["1"],["5","3","4"],["2","6"]

Try this. It loops through array1 and pushes each item into a temporary array. When the item is found in array2 the temporary array is pushed into the final array and then reset.

var array1 = ["1", "5", "3", "4", "2", "6"];
var array2 = ["2", "5"];

var newArray = [];
var currArray = [];

for (let i = 0; i < array1.length; i++) {

  // Item exists in array2. Add to newArray and reset currArray
  if (i > 0 && array2.includes(array1[i])) {
    newArray.push(currArray);
    currArray = [];
  }

  currArray.push(array1[i]);
}

newArray.push(currArray); // Add final currArray to newArray

console.log(newArray); // print result

On a side note, it's best not to use for..in to iterate through an array if index order is important (as in your case) as it does not return the indexes in any particular order. More info here.

发布评论

评论列表(0)

  1. 暂无评论