1

Write a function named "json_filter" that takes a JSON formatted string as a parameter in the format of an array of objects where each object has keys "mass", "density", "temperature", and "velocity" and each key maps to a floating point number. This function should return the input as a JSON string in the same format but with only the objects with temperature greater than 31.92

function json_filter(format){

  var array=JSON.stringify(format);
  var array2 = [];

  for (var i = 0; i<array.length;i++){
      if (array.i.temperature>31.92){
         array2.push(array[i]);
         return JSON.parse(array2);          
      }  
  }

}

When I run this, this is what it says:

error on input ['[{"velocity": 11.33, "mass": 14.56, "density": 165.09, "temperature": 29.92}, {"velocity": 57.86, "mass": 52.23, "density": 770.6, "temperature": 35.61}, {"velocity": 62.23, "mass": 84.85, "density": 85.22, "temperature": 51.66}, {"velocity": 16.63, "mass": 51.23, "density": 995.61, "temperature": 10.27}, {"velocity": 31.16, "mass": 71.76, "density": 967.53, "temperature": 50.43}, {"velocity": 14.35, "mass": 0.92, "density": 808.42, "temperature": 69.32}, {"velocity": 85.43, "mass": 41.07, "density": 899.84, "temperature": 51.05}]']: TypeError: Cannot read property 'temperature' of undefined

I'm not sure how to fix this, if anyone could help I'd greatly appreciate it.

1
  • You don't have to stringify it. Actually, you have to do the opposite if it's a string. Commented Nov 26, 2018 at 18:09

2 Answers 2

2
function json_filter(format){

  var array=JSON.parse(format);
  var array2 = [];

  for (var i = 0; i<array.length;i++){
     if (array[i]["temperature"]>31.92){
        array2.push(array[i]);
     }
  }
  return JSON.stringify(array2);
}

(answer solved as above, but can't mark it answered because I'm new here, for 2 more days.)

Sign up to request clarification or add additional context in comments.

2 Comments

You might want to use .map(), .filter() or .reduce() functions for anything related to iterables (including arrays) in javascript. Reduces a lot of code.
I'm still fairly new to python, actually coding in general (first year of it in my college), but I'm really enjoying it, I'll definitely look those up to learn how to use them.
0

Try this.

function json_filter(format){
  let arr = JSON.parse(format);
  return JSON.stringify(arr.filter(obj=>obj.temperature>31.92));
}

if you can't use es6 or

let json_filter = (format) =>{
  let arr = JSON.parse(format);
  return JSON.stringify(arr.filter({temperature}=>temperature>31.92));
}

if you can use es6. Note that the first one solution will work for es6 as well but not the other way around.

6 Comments

format is the string your last method was the one im trying to use but it doesnt work, it returns values like [('mass:68.07')] when it wants "[("mass" : 68.07)]"
no it gives a proper output, but i think it was it to be returned into a string if im reading the error correctly
Okay. So what's the problem you are facing? You want a string instead of an object?
@codernoob3232 if it solves your problem, please accept the answer. Welcome to stackoverflow! :-)
says i have to wait 2 days haha
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.