本文介绍了jQuery的grep或地图对象数组与多个搜索标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JSON中有一个价目表:

  {Products:[{AdminID:137,ProduktID:07.1434, itemColor:0000,5030,MalKode:1-3,...} {AdminID:6,ProduktID:07.1436,itemName:Repaplastgrå,itemColor:0070,MalKode: 1-3,...} {AdminID:146,ProduktID:90.0905,itemName:Mixer Gun,itemColor:null,MalKode:,...} {AdminID:89,ProduktID:02.0135,itemName: Repaplast Primer NEW FORMULA,itemColor:,MalKode:5-3,...}]} 





假设我想根据字段 ProduktID

限制仅显示特定产品的列表, code>(这是唯一的)。

07.1438,01,1340,05,04531,02.0135



我怎样才能 .grep 。 map 基于这些输入的对象数组?
我必须逐一搜索,然后将结果合并到另一个对象中吗?或者有一个更快的方法?

解决方案您可以使用 array#filter array#find 。这将导致基于 product id 字符串过滤产品



,产品ID:07.1438,itemName:Repaplast,itemColor:0000,5030,MalKode:1-3,},{AdminID:6,产品ID:07.1436,itemName:Repaplastgrå :0070,MalKode:1-3,},{AdminID:146,ProduktID:90.0905,itemName:Mixer Gun,itemColor:null,MalKode:,},{AdminID:89,ProduktID :02.0135,itemName:Repaplast Primer NEW FORMULA,itemColor:,MalKode:5-3,}]; var result = products.filter(o => productIds.split(',')。 find(productId => o.ProduktID === productId.trim())); console.log(result);


I have a price list in JSON:

  {Products: [{AdminID: 137, ProduktID: "07.1434", itemName: "Repaplast", itemColor: "0000, 5030", MalKode: "1-3", …}{AdminID: 6, ProduktID: "07.1436", itemName: "Repaplast grå", itemColor: "0070", MalKode: "1-3", …}{AdminID: 146, ProduktID: "90.0905", itemName: "Mixer Gun", itemColor: null, MalKode: "", …}{AdminID: 89, ProduktID: "02.0135", itemName: "Repaplast Primer NEW FORMULA", itemColor: "", MalKode: "5-3", …}]}

The list is about 400 products long.

Say I want to limit the list showing only specific products based on the field ProduktID (which is unique). Let's say I have a list of products in an offer:

07.1438, 01, 1340, 05, 04531, 02.0135

Where comma is the separator.

How can I .grep or .map the objects array based on these inputs?Do I have to search one by one and then combine the results into another object? Or is there a faster way?

解决方案

You can use array#filter and array#find. It will result in filtering the products based on the product ids string.

const productIds = '07.1438, 01,1340, 05,04531, 02.0135';

const products = [{AdminID: 137, ProduktID: "07.1438", itemName: "Repaplast", itemColor: "0000, 5030", MalKode: "1-3",},{AdminID: 6, ProduktID: "07.1436", itemName: "Repaplast grå", itemColor: "0070", MalKode: "1-3",},{AdminID: 146, ProduktID: "90.0905", itemName: "Mixer Gun", itemColor: null, MalKode: "",},{AdminID: 89, ProduktID: "02.0135", itemName: "Repaplast Primer NEW FORMULA", itemColor: "", MalKode: "5-3",}];

var result = products.filter( o => productIds.split(',').find(productId => o.ProduktID === productId.trim()));

console.log(result);

这篇关于jQuery的grep或地图对象数组与多个搜索标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:39
查看更多