简体   繁体   中英

Filtering data in a JSON array

I have an JSON array like this:

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:
    a. The values of "a" should be unique,
    b. The count of values of "a" should be >= 3 ; (eg: ABABABAB = 8 )
  2. Fetch the length of each string in the filtered array and sum it. (eg: ["ABABABA", "ABABA"] --> 12)

What I've done so far..

a. for a should be unique:

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

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

b. for >= 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:

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

 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 )

  2. join() the results of that into a single string

  3. Get the .length of that string

 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 ).

.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:

"ABABABABABAABACDA"

3. And finally, .length to give us the amount of letters in that string:

17

using 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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