根据内容切片阵列

时间:2015-11-16 19:48:55

标签: javascript arrays

我有一个复杂的任务,即将数据数组切片和切割成合理的JSON对象。第一步是根据元素的内容将数组切片为更小的数组。

在这个简化版本中,我想将一个大数组分解成一系列由单词&#34定义的较小数组;"

鉴于此数组:

that, thing, thing, that, thing, that, thing, thing, thing

我想回来:

[that, thing, thing],
[that, thing],
[that, thing, thing, thing],

5 个答案:

答案 0 :(得分:1)

var arr = ['that', 'thing', 'thing', 'that', 'thing', 'that', 'thing', 'thing', 'thing'];
var subArrays = [];

var subArrayItem = [];
arr.forEach(function(arrItem) {
    if(arrItem == 'that') {
        if(subArrayItem.length) // avoid pushing empty arrays
            subArrays.push(subArrayItem)
        subArrayItem = []
    }
    subArrayItem.push(arrItem)
})

if(subArrayItem.length) // dont forget the last array
    subArrays.push(subArrayItem)

答案 1 :(得分:1)

使用Array.reduce()很容易做到:

var array = ["that", "thing", "thing", "that", "thing", "that", "thing", "thing", "thing"];

console.log(array.reduce(function(prev, cur, idx, arr) {
    if (cur === "that") {
      // start a new sub-array
      prev.push(["that"]);
    }
    else {
      // append this token onto the current sub-array
      prev[prev.length - 1].push(cur);
    }

  return (prev);
}, []));

JSFiddle

答案 2 :(得分:0)

这将是手工作业。我建议indexOf(找到"那")和splice的组合(以删除相关的子数组)。

 public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.imageButton:
                if(food_pressed)
                {
                    ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
                    food_button.setImageResource(R.drawable.pressed_food);
                    tags.add("food");
                    food_pressed = false;
                    break;
                }
                else
                {
                    ImageButton food_button = (ImageButton) findViewById(R.id.imageButton);
                    food_button.setImageResource(R.drawable.icon_food);
                    tags.remove("food");
                    food_pressed = true;
                    break;
                }

        case R.id.imageButton9:
            ImageButton done_button = (ImageButton) findViewById(R.id.imageButton9);
            done_button.setImageResource(R.drawable.pressed_done);
            Global_Class.getInstance().getValue().tags = tags;
            //Toast.makeText(getApplicationContext(),Global_Class.getInstance().getValue().tags.toString(),Toast.LENGTH_SHORT).show();
            startActivity(toDescription);
            break;

etc...
}

答案 3 :(得分:0)

快速reduce应该修复它:

var src = ["that", "thing", "thing", "that", "thing", "that", "thing", "thing", "thing"]

var dest = src.reduce(function(p,d) {
    if (d === "that") {
        p.push([d]);
    } else {
        p[p.length-1].push(d);
    }
    return p;
},[]);

$("#json").text(JSON.stringify(dest));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="json"></div>

答案 4 :(得分:0)

您可以使用lastIndexOf,slice和unshift。

function test (a, b, item) {
  var i = a.lastIndexOf(item);
  while (i != -1) {
    b.unshift(a.slice(i));
    a = a.slice(0, i);
    i = a.lastIndexOf(item);
  }
  return b;
}

var arr1 = [1, 2, 3, 1, 2, 1, 2, 3, 4];
var arr2 = [];

test(arr1, arr2, 1);
console.log(arr2);

使用nodejs运行结果是:

[ [ 1, 2, 3 ], [ 1, 2 ], [ 1, 2, 3, 4 ] ]