-1

I want to filter my JSON in such a way that I only want to have data with "category": 'two'.

Data:

var json = [
             {"id": 1,
              "category":'one'; 
             }
             {"id": 1,
              "category":'two'; 
             }
             {"id": 1,
              "category":'two'; 
             }
             {"id": 1,
              "category":'three'; 
             }
           ]
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
abhi
  • 1,920
  • 6
  • 24
  • 27
  • 1
    Please learn [the difference between JSON and Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). – str Dec 03 '18 at 14:52
  • 1
    @str, that's not even an array of objects. It's just one big syntax error. – Federico klez Culloca Dec 03 '18 at 14:53
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – epascarello Dec 03 '18 at 14:53

1 Answers1

0

The data is not valid. Objects inside an array and each key-value pair should be separated by comma (,). Correct that then use filter():

var json = [{"id": 1,
  "category":'one' 
  },{"id": 1,
  "category":'two' 
  },{"id": 1,
  "category":'two' 
  },{"id": 1,
  "category":'three'
  }
]
var res = json.filter(i => i.category == 'two');
console.log(res);
Mamun
  • 66,969
  • 9
  • 47
  • 59