以特定顺序创建新数组

时间:2017-06-07 10:14:52

标签: javascript angularjs

一旦解决了特定的承诺,我希望循环遍历响应中的每个项目,并按特定顺序将它们推送到一个数组中。

  1. status-start
  2. status-in-progress
  3. status-completed
  4. 我该怎么做呢?到目前为止,这是我的代码:

    dashService.getBatches(refresh)
    .then(function (response) {
      if (response.error) {
        console.log("No data received");
      } else {
        angular.forEach(response.items, function (item) {
          if (item.status === 'start') {
            currentBatches.push(job);
          }
        });
      }
    });
    

4 个答案:

答案 0 :(得分:2)

这个怎么样

var newA=dataList.filter(x=>x.status=='status-start').concat(dataList.filter(x=>x.status=='status-in-progress')).concat(dataList.filter(x=>x.status=='status-completed'))

答案 1 :(得分:1)

收到回复后,请执行以下操作:

    dataList = response.items;
    var currentBatches = [];

    var statusStartItems = dataList.filter(function(item){ return item.status === 'status-start';});
    //Loop through status-start items and push into currentbatches array
    statusStartItems.each(function(item){
      currentBatches.push(item);
    }

    var statusInItems = dataList.filter(function(item){ return item.status === 'status-in';});
    //Loop through status-in items and push into currentbatches array
    statusInItems .each(function(item){
      currentBatches.push(item);
    }

..等等

答案 2 :(得分:0)

我会创建一个可用选项及其相应顺序的地图,然后根据该地图对您的值进行排序。然后,一旦排序迭代,阵列将它们推入目标数组。那个或者你可以在排序后将它们连接起来以生成一个新数组。

SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\xE0\xAE\x95\xE0\xAF\x8A...' for column 'occupation' at row 1 (SQL: insert into `occupation` (`occupation`, `created_by`, `updated_at`, `created_at`) values (கொத்தனார், 1, 2017-06-07 10:14:58, 2017-06-07 10:14:58))

Heres a fiddle

显然你需要改变对象结构等,这只是一个概念证明。这也是用ES6编写的,所以没有IE,但它可以很容易地用ES5编写。

编辑:我看到您的排序基于子属性'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 更新了演示以反映出来。

https://jsfiddle.net/k0m2s1yt/2/

答案 3 :(得分:0)

作为一个选项,您还可以拥有3个不同的数组,这些数组可以连接成结果1:



public class ImageTargetTracker {
    public static interface Listner {
        public void onButtonClicked(int index);
    }
    private Listner mListner;
    protected Activity mCurrentActivity;
    public void setActivity(Activity activity, Listner listner)
    {
        mCurrentActivity = activity;
        mListner = listner;

        mCurrentActivity.runOnUiThread(new Runnable() {
            public void run() {
                LayoutInflater inflater = mCurrentActivity.getLayoutInflater();
                Resources resources =  mCurrentActivity.getResources();
                String pkgName = mCurrentActivity.getPackageName();

                int id = resources.getIdentifier("camera_layout", "layout", pkgName);
                View view = inflater.inflate(id, null);

                mCurrentActivity.addContentView(view, param);
                //INITIALIZING UI ELEMENTS HERE (DISPLAYED ON TOP OF CAMERA)
    }


    public void OnMarkerFound(){

        mCurrentActivity.runOnUiThread(new Runnable() {
            public void run() {
                //Showing some UI elements
            }
        });
    }
}