在ramdajs中设置对象数组的值

时间:2016-07-05 13:20:54

标签: javascript arrays ramda.js

这是针对ramdajs大师的。在下面显示的代码片段中考虑一组对象 arr 。 要设置第一个对象的值,比如6,使用镜头, 人们会期望结果是一个数组。然而,这似乎不是这种情况,而是结果是一个对象。 例如:

import R from 'ramda'
let arr = [{a: 1}, {a: 2}, {a: 3}]
let aLens = R.lensPath([0, 'a'])
let result = R.set(aLens, 6, arr)

预期结果:

[{a: 6}, {a: 2}, {a: 3}]

实际结果:

{0: {a: 6}, 1: {a: 2}, 2: {a: 3}}

获取预期数组的一种方法是从对象中提取值:

result = R.values(result)

是否有更好的方法在对象数组中设置值,以便结果也是数组?

1 个答案:

答案 0 :(得分:4)

使用lensIndex

            class GetNotesFromDbTask extends AsyncTask<Void, Void, Boolean> {

            @Override
            protected Boolean doInBackground(Void... params) {
      SQLiteDatabase db = helpers.getReadableDatabase();
            String[] projection = {
                    NoteContract.FeedEntry._ID,
                    NoteContract.FeedEntry.NOTES_TITLE,
                    NoteContract.FeedEntry.NOTES_ID,
                    NoteContract.FeedEntry.NOTES_BODY,

            };

            String sortOrder =
                    NoteContract.FeedEntry.NOTES_ID + " DESC";

            Cursor cursor = db.query(
                    NoteContract.FeedEntry.TABLE_NAME,  // The table to query
                    projection,                               // The columns to return
                    null,                                // The columns for the WHERE clause
                    null,                            // The values for the WHERE clause
                    null,                                     // don't group the rows
                    null,                                     // don't filter by row groups
                    sortOrder                                 // The sort order
            );


            if(cursor.getCount()>0) //here i am getting error

            {

            }
                return null;
            }

            @Override
            protected void onPreExecute() {
                ProgressDialog dialog = new ProgressDialog(NoteDetail.this);
                dialog.setTitle("Loading");
                dialog.setMessage("Please wait..");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);
                dialog.show();
            }

            }  // this line add asynctask class end 

          protected void onCreate(Bundle savedInstanceState) {

            GetNotesFromDbTask  gnfd=new GetNotesFromDbTask ();
            //asynctask run onpre ->onbackground->onpost
gnfd.execute();


        }