使用map返回es6中的新对象

时间:2017-03-27 16:01:05

标签: javascript

我试图在一系列对象中循环以取出" leaf"对象并创建一个新的对象数组。

我的代码如下所示:

public class GridChildAdapter extends BaseAdapter {

private EditText editText;
private GridView gridViewParent;
private Context context;
private List<String> character;
private GridParentAdapter gridParentAdapter;

public  GridChildAdapter(Context context, List<String> character
,GridParentAdapter gridParentAdapter){
    this.context = context;
    this.character = character;
    this.gridParentAdapter = gridParentAdapter;
}
@Override
public int getCount() {
    return character.size();
}

@Override
public Object getItem(int i) {
    return character;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup
parent) {                      
    LayoutInflater inflater =(LayoutInflater)context.getSystemService                  
    (Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.item_grid_child, parent,
    false); 

    editText = (EditText) convertView.findViewById(R.id.edit_text);
    editText.setText(character.get(position));
    editText.setClickable(true);
    editText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(MotionEvent.ACTION_UP == event.getAction()) {
                InputMethodManager im = (InputMethodManager) 
                context.getSystemService
                (Context.INPUT_METHOD_SERVICE);
                im.showSoftInput(editText, 0);
                editText.requestFocus();

            }
            return false;
        }
    });
    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int
        i,int i1, int i2) { 

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int
        i1, int i2) { 
            if (editText.getText().toString() != "null") {
               gridParentAdapter.setItemChar
               (position,charSequence.toString());
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
    return convertView;
}

public String getEditTextValue(EditText editText){
    String string = "";
    if(editText != null){
        string =  editText.getText().toString();
    }
    return string;
   }
}

let topic_data_leaf = topicData.hits.hits.map((hit) => { return hit._source }) console.log("topic leaf": topic_data_leaf); 具有以下结构:

topic_data_leaf

我想拉出hits: { hits: [ {_id: "id12345" _index: "index1" _score: 5.7439504 _source: {field1: val1, field2: val2} _type: "kafka" } [ } 字段中的对象并创建这些对象的新数组。

1 个答案:

答案 0 :(得分:0)

除了你遇到的一些小错误之外,你的代码应该可以正常工作。

let topic_data_leaf = topicData.hits.hits.map((hit) => { return hit._source })

console.log("topic leaf:", topic_data_leaf); // the colon shouldn't be outside the string

还要确保您的对象实际上是正确的:

hits: {
    hits: [
     {_id: "id12345"
      _index: "index1"
      _score: 5.7439504
      _source: {field1: val1, field2: val2}
      _type: "kafka"
     }   
    ] // this line was '[' in your code
}

这应该现在正常运作。