Lodash检查数组中的任何对象是否包含来自另一个数组的id匹配

时间:2018-01-17 04:52:23

标签: javascript angularjs lodash

我有一个用户数组,如:

      public class ProductCartAdapter extends SectionedRecyclerViewAdapter<RecyclerView.ViewHolder> {
        private List<ProductCart> allData;

        public ProductCartAdapter(List<ProductCart> data) {
            this.allData = data;
        }

        @Override
        public int getSectionCount() {
            return allData.size();
        }

        @Override
        public int getItemCount(int section) {
            return allData.get(section).getItemList().size();
        }

        @Override
        public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int section) {
            String sectionName = allData.get(section).getName();
            SectionViewHolder sectionViewHolder = (SectionViewHolder) holder;
            sectionViewHolder.tvProductName.setText(sectionName);
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int section, final int relativePosition, final int absolutePosition) {
            final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            itemViewHolder.tvProductName.setText(allData.get(section).getItemList().get(relativePosition).getOptionName());
            itemViewHolder.tvProductPrice.setText("+ $" + allData.get(section).getItemList().get(relativePosition).getOptionPrice());

            **// what I have to code here for selecting only one checkbox (not from addons only from base and sauce)**

            itemViewHolder.cbProduct.setTag(section);
            itemViewHolder.cbProduct.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetCategoryWiseData(section);
                    Integer pos = (Integer) itemViewHolder.cbProduct.getTag();

                    if (allData.get(section).getItemList().get(relativePosition).isSelected()) {
                        allData.get(pos).getItemList().get(relativePosition).setSelected(false);
                    } else {
                        allData.get(pos).getItemList().get(relativePosition).setSelected(true);
                    }
    notifyItemChanged(pos);
                }
            });

    *********************************************************

        //code for checked/unchecked
        if(allData.get(section).getItemList().get(relativePosition).getSelected()){
        itemViewHolder.cbProduct.setChecked(true);
        }else{
        itemViewHolder.cbProduct.setChecked(false);
        }

    **************************************************************
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, boolean header) {
            View v = null;
            if (header) {
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_section_cart, parent, false);
                return new SectionViewHolder(v);
            } else {
                v = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_cart_item, parent, false);
                return new ItemViewHolder(v);
            }

        }

        // SectionViewHolder Class for Sections
        public class SectionViewHolder extends RecyclerView.ViewHolder {
            final TextView tvProductName;

            public SectionViewHolder(View itemView) {
                super(itemView);
                tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
            }
        }

        // ItemViewHolder Class for Items in each Section
        public class ItemViewHolder extends RecyclerView.ViewHolder {

            final TextView tvProductName, tvProductPrice;
            CheckBox cbProduct;

            public ItemViewHolder(final View itemView) {
                super(itemView);
                tvProductName = (TextView) itemView.findViewById(R.id.tvProductName);
                tvProductPrice = (TextView) itemView.findViewById(R.id.tvProductPrice);
                cbProduct = (CheckBox) itemView.findViewById(R.id.cbProduct);
            }
        }

private void  resetCategoryWiseData(int section){
    for(int i=0;i<allData.get(section).getItemList().size();i++){
         allData.get(section).getItemList().get(i).setSelected(false);
     }
  }

add this line  **notifyItemChanged(pos);** in click listener of checkox.

and add some code that i have already written in above for ( checkbox is checked or not )


if any issue then tell me.Happy Coding.:)

另一个数组selectedUsers包含来自上面数组的一些对象,如:

var users = [{
 id: 1,
 name: 'ABC',
 isDisplay: true
}, {
 id: 2,
 name: 'XYZ',
 isDisplay: true
}, {
 id: 3,
 name: 'JKL',
 isDisplay: true
}];

不是用lodash,我想识别第二个数组中哪个对象与var selectedUsers = [{ id: 1, name: 'ABC' }, { id: 3, name: 'JKL' }]; 匹配。

ID

我试图将整个对象与 _.each(users, (_u) => { if(selectedUsers.includes(_u)) { _u.isDisplay = false; } else { _u.isDisplay = true; } }); 匹配,但它有效,因为我使用的是angularjs,所以angular将一些$$ hashkey与对象放在一起,所以它不匹配,还有其他办法吗此

2 个答案:

答案 0 :(得分:0)

&#13;
&#13;
var users = [{
 id: 1,
 name: 'ABC',
 isDisplay: true
}, {
 id: 2,
 name: 'XYZ',
 isDisplay: true
}, {
 id: 3,
 name: 'JKL',
 isDisplay: true
}];

var selectedUsers = [{
 id: 1,
  name: 'ABC'
 },
 {
  id: 3,
  name: 'JKL'
}];

var intersection = _.intersectionBy(users, selectedUsers, 'id');

console.log(intersection);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

创建Set个选定的ID(selectedUsersIds)。使用Array#forEach迭代users数组,通过检查isDisplay集中是否存在id来指定selectedUsersIds的值:

&#13;
&#13;
const users = [{"id":1,"name":"ABC","isDisplay":true},{"id":2,"name":"XYZ","isDisplay":true},{"id":3,"name":"JKL","isDisplay":true}];

const selectedUsers = [{"id":1,"name":"ABC"},{"id":3,"name":"JKL"}];

const selectedUsersIds = new Set(selectedUsers.map(({ id }) => id));

users.forEach((u) => u.isDisplay = selectedUsersIds.has(u.id));

console.log(users);
&#13;
&#13;
&#13;