如何遍历对象数组并使用 linq.js 将 属性 与变量匹配
how to loop through array of objects and match property with variable using linq.js
我正在尝试使用 linq.js 通过 属性 匹配定位对象。 属性 我需要匹配的是一个数组对象,然后嵌套在数组数组中。
Json我正在循环
customSeries = [{"name":"Chantal Hamlet - Green Castle Homes","subId":"10223","bldId":"13551","data":[[179900,1386],[214900,1440],[194500,1496],[217900,1504],[189900,1542],[184900,1546],[192500,1570],[189900,1576],[191900,1598],[204900,1626],[219900,1651],[212900,1704],[214900,1787],[219900,1837],[224900,1857]],"removeByNames":[["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"]]},{"name":"Ella Sea Condos - Sahnow Construction","subId":"9761","bldId":"27380","data":[[199900,1500]],"removeByNames":[["null"]]},{"style":"smooth","color":"blue","data":[[20000,200],[40000,400],[[40000,400]],[30000,300],[[30000,300]]],"name":"Subject Property","removeByNames":[["Product1"],["Product2"],["Product3"]]}]
要匹配的项目
var modelName = 'Product2'
javascript
remove: function (e) {
removeByNames = []
var modelName = e.model.name;
// Enumerate through the series
var customSeriesSearchResults = Enumerable.From(customSeries)
.Where(function (item) {
// Enumerate through the series.removeByNames
return Enumerable.From(item.removeByNames).Any(function (modelName) {
// Find matching removeByNames.name
return Enumerable.From(removeByNames).Contains(modelName);
})
})
.ToArray();
}
使用纯 js,您可以遍历每个对象,然后对 removedByNames
数组执行另一个循环。
然后在扁平数组上使用 array.indexOf()
,如果找到,它将 return 字符串在数组中的位置。如果没有找到它会 return -1.
您可以对 linqJs
使用相同的方法。不确定在 linqjs 中是否有更好的方法。但它也适用于 linq。
请查看下面和此处的演示代码 jsfiddle。
(我已将虚拟数据添加到您的 json 以具有相同 modelName
的第二个对象。)
customSeries = [{
"name": "Chantal Hamlet - Green Castle Homes",
"subId": "10223",
"bldId": "13551",
"data": [
[179900, 1386],
[214900, 1440],
[194500, 1496],
[217900, 1504],
[189900, 1542],
[184900, 1546],
[192500, 1570],
[189900, 1576],
[191900, 1598],
[204900, 1626],
[219900, 1651],
[212900, 1704],
[214900, 1787],
[219900, 1837],
[224900, 1857]
],
"removeByNames": [
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"]
]
}, {
"name": "Ella Sea Condos - Sahnow Construction",
"subId": "9761",
"bldId": "27380",
"data": [
[199900, 1500]
],
"removeByNames": [
["null"]
]
}, {
"style": "smooth",
"color": "blue",
"data": [
[30000, 500],
[40000, 400],
[
[40000, 400]
],
[50000, 800],
[
[50000, 800]
]
],
"name": "Subject Property",
"removeByNames": [
["Product1"],
["Product2"],
[
["Product2"]
],
["Product3"],
[
["Product3"]
]
]
}, { // add another item with product2
"style": "smooth",
"color": "gray",
"data": [
[30000, 500],
[40000, 400],
[
[40000, 400]
],
[50000, 800],
[
[50000, 800]
]
],
"name": "Subject Property dummy data",
"removeByNames": [
[
["Product2"]
],
["Product3"],
[
["Product3"]
]
]
}];
console.log(customSeries);
/*customersWithProduct2 = customSeries.filter(function(customer){
console.log('Cust', customer);
return customer.modelname === 'Product2';})*/
var modelName = 'Product2'
var customers = [];
// flatten code from here
var flatten = function (arr) {
return arr.reduce(function (prev, cur) {
var more = [].concat(cur).some(Array.isArray);
return prev.concat(more ? flatten(cur) : cur);
}, []);
};
//console.log('flat test', flatten(['dummy', ['1','2'], 'dummy2']));
//console.log('flat test', flatten([['1']]));
// with-out linqjs
customSeries.forEach(function (obj) {
//console.log(obj);
var foundItem, flattened;
obj.removeByNames.some(function (name) {
flattened = flatten(name);
//console.log('name', name, flattened, flattened.indexOf(modelName) > -1, obj);
foundItem = flattened.indexOf(modelName) > -1 ? obj : undefined;
return !!foundItem; //!! creates bool if true exits the loop
});
//console.log('found', foundItem);
if (foundItem) {
customers.push(foundItem);
}
});
console.log('pure js', customers);
$('body').append($('<pre/>').html(JSON.stringify(customers, null, 2))); // jquery just to log the object to the output
// with linqjs
removeByNames = []
var flatArray = [];
// Enumerate through the series
var customSeriesSearchResults = Enumerable.From(customSeries)
.Where(function (item) {
// Enumerate through the series.removeByNames
return Enumerable.From(item.removeByNames).Any(function (name) {
// Find matching removeByNames.name
// console.log('loop', Enumerable.From(item.removeByNames)
// ,Enumerable.From(removeByNames).Contains(modelName));
flatArray = flatten(Enumerable.From(item.removeByNames).ToArray());
return flatArray.indexOf(modelName) > -1; //Enumerable.From(flatArray).Contains(modelName);
})
}).ToArray();
console.log('with linqjs', customSeriesSearchResults);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>
我正在尝试使用 linq.js 通过 属性 匹配定位对象。 属性 我需要匹配的是一个数组对象,然后嵌套在数组数组中。
Json我正在循环
customSeries = [{"name":"Chantal Hamlet - Green Castle Homes","subId":"10223","bldId":"13551","data":[[179900,1386],[214900,1440],[194500,1496],[217900,1504],[189900,1542],[184900,1546],[192500,1570],[189900,1576],[191900,1598],[204900,1626],[219900,1651],[212900,1704],[214900,1787],[219900,1837],[224900,1857]],"removeByNames":[["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"],["null"]]},{"name":"Ella Sea Condos - Sahnow Construction","subId":"9761","bldId":"27380","data":[[199900,1500]],"removeByNames":[["null"]]},{"style":"smooth","color":"blue","data":[[20000,200],[40000,400],[[40000,400]],[30000,300],[[30000,300]]],"name":"Subject Property","removeByNames":[["Product1"],["Product2"],["Product3"]]}]
要匹配的项目
var modelName = 'Product2'
javascript
remove: function (e) {
removeByNames = []
var modelName = e.model.name;
// Enumerate through the series
var customSeriesSearchResults = Enumerable.From(customSeries)
.Where(function (item) {
// Enumerate through the series.removeByNames
return Enumerable.From(item.removeByNames).Any(function (modelName) {
// Find matching removeByNames.name
return Enumerable.From(removeByNames).Contains(modelName);
})
})
.ToArray();
}
使用纯 js,您可以遍历每个对象,然后对 removedByNames
数组执行另一个循环。
然后在扁平数组上使用 array.indexOf()
,如果找到,它将 return 字符串在数组中的位置。如果没有找到它会 return -1.
您可以对 linqJs
使用相同的方法。不确定在 linqjs 中是否有更好的方法。但它也适用于 linq。
请查看下面和此处的演示代码 jsfiddle。
(我已将虚拟数据添加到您的 json 以具有相同 modelName
的第二个对象。)
customSeries = [{
"name": "Chantal Hamlet - Green Castle Homes",
"subId": "10223",
"bldId": "13551",
"data": [
[179900, 1386],
[214900, 1440],
[194500, 1496],
[217900, 1504],
[189900, 1542],
[184900, 1546],
[192500, 1570],
[189900, 1576],
[191900, 1598],
[204900, 1626],
[219900, 1651],
[212900, 1704],
[214900, 1787],
[219900, 1837],
[224900, 1857]
],
"removeByNames": [
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"],
["null"]
]
}, {
"name": "Ella Sea Condos - Sahnow Construction",
"subId": "9761",
"bldId": "27380",
"data": [
[199900, 1500]
],
"removeByNames": [
["null"]
]
}, {
"style": "smooth",
"color": "blue",
"data": [
[30000, 500],
[40000, 400],
[
[40000, 400]
],
[50000, 800],
[
[50000, 800]
]
],
"name": "Subject Property",
"removeByNames": [
["Product1"],
["Product2"],
[
["Product2"]
],
["Product3"],
[
["Product3"]
]
]
}, { // add another item with product2
"style": "smooth",
"color": "gray",
"data": [
[30000, 500],
[40000, 400],
[
[40000, 400]
],
[50000, 800],
[
[50000, 800]
]
],
"name": "Subject Property dummy data",
"removeByNames": [
[
["Product2"]
],
["Product3"],
[
["Product3"]
]
]
}];
console.log(customSeries);
/*customersWithProduct2 = customSeries.filter(function(customer){
console.log('Cust', customer);
return customer.modelname === 'Product2';})*/
var modelName = 'Product2'
var customers = [];
// flatten code from here
var flatten = function (arr) {
return arr.reduce(function (prev, cur) {
var more = [].concat(cur).some(Array.isArray);
return prev.concat(more ? flatten(cur) : cur);
}, []);
};
//console.log('flat test', flatten(['dummy', ['1','2'], 'dummy2']));
//console.log('flat test', flatten([['1']]));
// with-out linqjs
customSeries.forEach(function (obj) {
//console.log(obj);
var foundItem, flattened;
obj.removeByNames.some(function (name) {
flattened = flatten(name);
//console.log('name', name, flattened, flattened.indexOf(modelName) > -1, obj);
foundItem = flattened.indexOf(modelName) > -1 ? obj : undefined;
return !!foundItem; //!! creates bool if true exits the loop
});
//console.log('found', foundItem);
if (foundItem) {
customers.push(foundItem);
}
});
console.log('pure js', customers);
$('body').append($('<pre/>').html(JSON.stringify(customers, null, 2))); // jquery just to log the object to the output
// with linqjs
removeByNames = []
var flatArray = [];
// Enumerate through the series
var customSeriesSearchResults = Enumerable.From(customSeries)
.Where(function (item) {
// Enumerate through the series.removeByNames
return Enumerable.From(item.removeByNames).Any(function (name) {
// Find matching removeByNames.name
// console.log('loop', Enumerable.From(item.removeByNames)
// ,Enumerable.From(removeByNames).Contains(modelName));
flatArray = flatten(Enumerable.From(item.removeByNames).ToArray());
return flatArray.indexOf(modelName) > -1; //Enumerable.From(flatArray).Contains(modelName);
})
}).ToArray();
console.log('with linqjs', customSeriesSearchResults);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>