2

I would like to tell a json object, to stringify the object that belongs to the array with this days date/time.

I have a php script which fetches a json object, with multiple arrays in it, which looks something like this:

data: Array(55)

    0: {date: "2017112700", p: Array(1)}
    1: {date: "2017112701", p: Array(1)}
    2: {date: "2017112702", p: Array(1)}

    and so forth...55 in all.

I am able to verify that my date variable (date_test) matches a date variable in the json array.

var date_test;

var index = json.data.findIndex(function(item, i){
  return item.date === date_test
});

console.log(index);

Lets say that console.log(index); gives my a '9'.

This shows me where date_test and item.date match.

Now this where I am stuck....I can stringify and whatever the information from the json arrays, but I would like stringify the object that matches my date_test. Anyone?

3
  • 1
    Why not JSON.parse it and find the actual data? Commented Nov 27, 2017 at 9:55
  • Do you want the complete object of which date matches? Commented Nov 27, 2017 at 9:57
  • The returned data does not look like JSON. Commented Nov 27, 2017 at 10:17

4 Answers 4

2

Get the object that machtes your search:

var obj = json.data[index]; //your previously found index

Then stringify it:

var json = JSON.stringify(obj);
Sign up to request clarification or add additional context in comments.

2 Comments

in case you just need to stringify the p array use this to grab it: json.data[index].p
This was it! Thanks...the simple answer! I just used for example: var json_temp = JSON.stringify(json.data[index].p[0]["temperature"]); and then console.log(json_temp);
1

You can use Array#find() method like this:

let date_test = "2017112701";

var searchObj = arr.find(function(item) {
  return item.date === date_test;
});

Then you can stringify it:

JSON.stringify(searchObj);

Demo:

var arr = [{
    date: "2017112700",
    p: []
  },
  {
    date: "2017112701",
    p: []
  },
  {
    date: "2017112702",
    p: []
  }
];

let date_test = "2017112701";

var searchObj = arr.find(function(item) {
  return item.date === date_test;
});
console.log(JSON.stringify(searchObj));

Comments

0

You can use jQuery.grep() to filter the json data as shown below

$(document).ready(function() {
  var myObj = [{
      date: "2017112700",
      p: ['a']
    },
    {
      date: "2017112701",
      p: ['b']
    },
    {
      date: "2017112702",
      p: ['c']
    }
  ];
  var mydate = '2017112701';
  var output = $.grep(myObj, function(value, index) {
    return value.date == mydate;
  });
  console.log(JSON.stringify(output));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

0

var json_temp = JSON.stringify(json.data[index].p[0]["temperature"]); and then console.log(json_temp);

Comments

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.