简体   繁体   English

过滤JSON数组中的数据

[英]Filtering data in a JSON array

I have an JSON array like this: 我有一个像这样的JSON数组:

json = [{
 "a":"ABABABAB",
 "b":30,
 "c":"available"
},
{
 "a":"A",
 "b":30,
 "c":"unavailable"
},
{
 "a":"AB",
 "b":29,
 "c":"available"
},
{
 "a":"ABAABACDA",
 "b":29,
 "c":"available"
},
{
 "a":"ABAABACDA",
 "b":29,
 "c":"available"
}];
  1. I have to filter the JSON data out based on following two conditions: 我必须根据以下两个条件过滤掉JSON数据:
    a. 一种。 The values of "a" should be unique, “ a”的值应该是唯一的,
    b. b。 The count of values of "a" should be >= 3 ; “ a”的值的计数应>= 3 (eg: ABABABAB = 8 ) (例如: ABABABAB = 8
  2. Fetch the length of each string in the filtered array and sum it. 获取过滤后的数组中每个字符串的长度并将其求和。 (eg: ["ABABABA", "ABABA"] --> 12) (例如: ["ABABABA", "ABABA"] -> 12)

What I've done so far.. 到目前为止我做了什么

a. 一种。 for a should be unique: 对于a应该是唯一的:

unique_a = [];
for (i = 0; i < json.length; i++) {

  if (unique_a.indexOf(json[i].a) === -1) {
    unique_a.push(json[i].a);
  }
}

b. b。 for >= 3 : 对于>= 3

var sort_3 = unique_a.filter(el => el.a.length >= 3);

For the total of all a.length , you can use a simple forEach() loop and add the lengths to a variable: 对于所有a.length的总数,可以使用一个简单的forEach()循环并将长度添加到变量中:

var total = 0;

unique_a.forEach(function(u) {
  total += u.length;
});

console.log(total);

you can use JS Array.map Array.fileter Array.reduce to achiveby chaining the methods 您可以通过链接方法使用JS Array.map Array.fileter Array.reduce实现

 function countUniqueTextLengths(arr) { return arr.map((a) => aa).filter((a, i, ar) => ar.indexOf(a) == i && a.length > 4).reduce((t, v) => t + v.length, 0) } //optimized function countUniqueTextLengths_2(arr) { var u = [], c = 0; arr.forEach(element => { if (u.indexOf(element.a) === -1 && element.a.length > 4) { u.push(element.a); c += element.a.length; } }); return c; } var json = [{ "a": "ABABABAB", "b": 30, "c": "available" }, { "a": "A", "b": 30, "c": "unavailable" }, { "a": "AB", "b": 29, "c": "available" }, { "a": "ABAABACDA", "b": 29, "c": "available" }, { "a": "ABAABACDA", "b": 29, "c": "available" } ]; console.log(countUniqueTextLengths(json)) console.log(countUniqueTextLengths_2(json)) 

This could be done in a few simple, chained steps: 这可以通过几个简单的链接步骤完成:

  1. reduce() the array to apply our filters (unique, and length > 3 ) reduce()数组以应用我们的过滤器(唯一,且length > 3

  2. join() the results of that into a single string join()将结果转换为单个字符串

  3. Get the .length of that string 获取该字符串的.length

 var json = [{ "a": "ABABABAB", "b": 30, "c": "available" }, { "a": "A", "b": 30, "c": "unavailable" }, { "a": "AB", "b": 29, "c": "available" }, { "a": "ABAABACDA", "b": 29, "c": "available" }, { "a": "ABAABACDA", "b": 29, "c": "available" }]; let result = json .reduce( (a,i) => !a.includes(ia) && ialength > 3 ? [...a, ia] : a , []) .join("") .length; console.log(result); 


Explanation 说明

1. .reduce() takes our existing array items ( i ) and creates a new one from them ( a ). 1. .reduce()获取我们现有的数组项( i ),并从它们中创建一个新项( a )。

.reduce( (a,i) => 
   !a.includes(i.a) && i.a.length > 3  //If not yet in our resulting array and length > 3
   ? [...a, i.a]  //Add it to the resulting array
   : a            //Else, leave the array alone
 , [])            //The initial value of our resulting array

This leaves us with: 这给我们留下了:

[
  "ABABABAB",
  "ABAABACDA"
]

2. We can then .join("") to combine it into a single string: 2.然后,我们可以将.join("")组合成一个字符串:

"ABABABABABAABACDA"

3. And finally, .length to give us the amount of letters in that string: 3.最后, .length给我们该字符串中的字母数量:

17

using lodash https://lodash.com/ 使用lodash https://lodash.com/

 var json = [{ "a":"ABABABAB", "b":30, "c":"available" }, { "a":"A", "b":30, "c":"unavailable" }, { "a":"AB", "b":29, "c":"available" }, { "a":"ABAABACDA", "b":29, "c":"available" }, { "a":"ABAABACDA", "b":29, "c":"available" }]; var uniq = _.uniqBy(json, 'a'); var result = _.filter(uniq, function(value, key) { return value.a.length >= 3; }); //console.log(result); var lengths = _.map(_.map(result, 'a'), function(str){ return str.length }); var sum = _.reduce(lengths, (a, b) => a + b, 0); console.log(sum); 
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM