使用underscore.js从数组中删除多个项目?

时间:2016-08-10 11:28:48

标签: javascript underscore.js

我有这个:

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.clist_item, null);
        holder = new ViewHolder();
holder.requestion_layout = (RelativeLayout) convertView.findViewById(R.id.requestion_layout);
holder.submitAnswerBtn = (RelativeLayout) convertView.findViewById(R.id.submit_answer_btn);
holder.questionTitle = (TextView) convertView.findViewById(R.id.question_title);
 //these are the widgets 
    holder.requestion_layout.setTag(position);
    holder.submitAnswerBtn.setTag(position);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


//trying to change visibility on click of Clickable Span 
 Spannable main_words = new SpannableString(items.get(position).getAnswer() + "");
        int color1 = ContextCompat.getColor(activity, R.color.light_black);
        main_words.setSpan(new ForegroundColorSpan(color1), 0, main_words.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color2 = ContextCompat.getColor(activity, R.color.tab_green);
        Spannable wordTwo = new SpannableString("Reply");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                //Toast.makeText(DummyActivity.this, "Click", Toast.LENGTH_LONG).show();
           Integer position_clicked = (Integer) v.getTag();
           //Widgets are not getting their positions here
                holder.requestion_layout.setVisibility(View.VISIBLE);
                holder.submitAnswerBtn.setVisibility(View.VISIBLE);
            }
        };
        wordTwo.setSpan(clickableSpan, 0, wordTwo.toString().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        wordTwo.setSpan(new ForegroundColorSpan(color2), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        wordTwo.setSpan(new StyleSpan(Typeface.ITALIC), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        CharSequence concat = TextUtils.concat(main_words, " ", wordTwo);
        holder.answerValue.setText(concat);
        holder.answerValue.setMovementMethod(LinkMovementMethod.getInstance());
        holder.answerValue.setHighlightColor(Color.TRANSPARENT);
        holder.answerValue.setVisibility(View.VISIBLE);
        holder.answerTitle.setVisibility(View.VISIBLE);
return convertView;
 }

我有另一个阵列:

var arrA = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];

如何使用underscore.js从arrA中删除属性ID与arrB相同的项目?

预期结果应为:

var arrB = [{id:1,other:'c'},{id:3,other:'d'}];

谢谢,

5 个答案:

答案 0 :(得分:3)

  

使用Array#filterArray#findIndex

var output = arrA.filter((el) => {
  return arrB.findIndex((elem) => {
    return elem.id == el.id;
  }) == -1;
});

单线:

arrA.filter((el) => (arrB.findIndex((elem) => (elem.id == el.id)) == -1));

var arrA = [{
  id: 1,
  name: 'a'
}, {
  id: 2,
  name: 'b'
}, {
  id: 3,
  name: 'c'
}];


var arrB = [{
  id: 1,
  other: 'c'
}, {
  id: 3,
  other: 'd'
}];

var op = arrA.filter(function(el) {
  return arrB.findIndex(function(elem) {
    return elem.id == el.id;
  }) == -1;
});
console.log(op);

使用Array#find

var arrA = [{
  id: 1,
  name: 'a'
}, {
  id: 2,
  name: 'b'
}, {
  id: 3,
  name: 'c'
}];


var arrB = [{
  id: 1,
  other: 'c'
}, {
  id: 3,
  other: 'd'
}];

var op = arrA.filter(function(el) {
  return !arrB.find(function(elem) {
    return elem.id == el.id;
  });
});
console.log(op);

答案 1 :(得分:1)

喜欢这个

var arrA = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
var arrB = [{id:1,other:'c'},{id:3,other:'d'}];
var keys = _.keys(_.indexBy(arrB, "id"));
var result = _.filter(arrA, function(v) {
   return !_.contains(keys, v.id.toString());
});
console.log(result)

希望这有帮助。

答案 2 :(得分:1)

在纯JavaScript中,如果在其他数组中找到forEach(),则可以使用splice()循环和id删除对象。

var arrA = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}];
var arrB = [{id:1,other:'c'},{id:3,other:'d'}];

var b = arrB.map(e => e.id);

arrA.forEach(function(e, i) {
  if(b.indexOf(e.id) != -1) arrA.splice(i, 1);
});

console.log(arrA);

答案 3 :(得分:0)

听起来你想要有所不同,但遗憾的是,它不适用于物体。相反,你可以试试这个:

{{1}}

您可以使用过滤器查找功能中的构建而不使用下划线。

答案 4 :(得分:0)

var arrA = [{id:1,name:'a'}, {id:2,name:'b'}, {id:3,name:'c'}];
var arrB = [{id:1,other:'c'}, {id:3,other:'d'}];

var res = arrB.reduce((acc, b) => {
  return acc.filter(({id}) => id !== b.id);
}, arrA);

// [{id:2,name:'b'}]
console.log(res);

相关问题