0

I am receiving data from another site with json API.

how can I get the data of the objects with the same id number.

I try as follows but it turns blank..

there are 10 objects with the same id number how can i get them.

The typeid number is the same but the specialvalue value is different.

   {"id":4620041,"active":true,"typeid":56,"specialvalue":"0.5","fields":{"magic":{"active":true,"type":"magic","value":"1.05"},"xmagic":{"active":true,"type":"xmagic","value":"7.5"}}}

{"id":4620045,"active":true,"typeid":56,"specialvalue":"1.5","fields":{"magic":{"active":true,"type":"magic","value":"3.05"},"xmagic":{"active":true,"type":"xmagic","value":"3.5"}}}

    if($test->typeid =="56")
   { 
    if (specialvalue == "0.5") {
   $alta = $test->fields->magic->value;
   $uste = $test->fields->xmagic->value;
    }
   }
   
   echo $alta - $uste ;

Thank you in advance for your help

https://i.hizliresim.com/Pl0LL7.jpg

Mert Fırat
  • 15
  • 2
  • 8

2 Answers2

0

Use .filter

var filteredData = data.filter(
  (el) => {
     return el.typeid===56 && el.specialvalue==="0.5";
  }
);

This will return only those elements with el.typeid===56 && specialvalue==="0.5"

void
  • 36,090
  • 8
  • 62
  • 107
  • Should we not close as duplicate instead? – Rajesh Feb 12 '18 at 08:05
  • @Rajesh Isn't the link you have provided is a duplicate of https://stackoverflow.com/questions/2722159/javascript-how-to-filter-object-array-based-on-attributes ? It's a chain you cant control and I was specifically trying to put the ES6 version here as most of the (older) answers are ES5 based. Also this doesnt justify your downvote. – void Feb 12 '18 at 08:09
  • @void agree that there is a chain of post that should be marked as duplicate but are not. But this does not mean we should do the same. We can try to minimize it. Also, downvote does not mean answer is wrong. It means it is not required as question is a dupe. Also its not about use arrow functions. Objective of this post is *how to filter data* and that is addressed in linked posts(*mine and yours*). – Rajesh Feb 12 '18 at 08:18
  • @Rajesh indeed it is about the use of ES6 here for me. Otherwise, you can do this using for loops but that wouldn't be the preferred approach. I didn't get any "Duplicate of ..." stating this answer which OP could get so I preferred posting it. :) – void Feb 12 '18 at 08:21
  • @MertFırat instead of `el.typeid===56 && el.specialvalue==="0.5";` use `el.typeid===5` as your condition and you will get all the array elements with `typeid` = 56. – void Feb 12 '18 at 08:22
  • not work.... , if($test->typeid =="56") { if (specialvalue == "0.5") { $alta = $test->fields->magic->value; $uste = $test->fields->xmagic->value; } } echo $alta - $uste ;; I tried this way, again the same :( – Mert Fırat Feb 12 '18 at 08:29
  • @MertFırat you are doing it using PHP and the tags you have used in your question is `Javascript` so ofcourse js code wont work in PHP. I would request you to modify your question accordingly. – void Feb 12 '18 at 09:16
0

Few observations :

  • Provided JSON is not a valid JSON.

    enter image description here

  • It should be an array of objects to filter the specific data as per the requirement.

Solution :

let jsonObj = [{
 "id": 5246752,
 "active": true,
 "typeid": 56,
 "specialvalue": "0.5",
 "fields": {
  "magic": {
   "active": true,
   "type": "magic",
   "value": "1.06"
  },
  "xmagic": {
   "active": true,
   "type": "xmagic",
   "value": "7.0"
  }
 }
}, {
 "id": 5246753,
 "active": true,
 "typeid": 56,
 "specialvalue": "1.5",
 "fields": {
  "magic": {
   "active": true,
   "type": "magic",
   "value": "1.35"
  },
  "xmagic": {
   "active": true,
   "type": "xmagic",
   "value": "3.0"
  }
 }
}, {
 "id": 5246754,
 "active": true,
 "typeid": 56,
 "specialvalue": "2.5",
 "fields": {
  "magic": {
   "active": true,
   "type": "magic",
   "value": "2.0"
  },
  "xmagic": {
   "active": true,
   "type": "xmagic",
   "value": "1.7"
  }
 }
}, {
 "id": 5246755,
 "active": true,
 "typeid": 56,
 "specialvalue": "3.5",
 "fields": {
  "magic": {
   "active": true,
   "type": "magic",
   "value": "3.45"
  },
  "xmagic": {
   "active": true,
   "type": "xmagic",
   "value": "1.25"
  }
 }
}, {
 "id": 5246756,
 "active": true,
 "typeid": 56,
 "specialvalue": "4.5",
 "fields": {
  "magic": {
   "active": true,
   "type": "magic",
   "value": "6.25"
  },
  "xmagic": {
   "active": true,
   "type": "xmagic",
   "value": "1.08"
  }
 }
}, {
 "id": 5246757,
 "active": true,
 "typeid": 56,
 "specialvalue": "5.5",
 "fields": {
  "magic": {
   "active": true,
   "type": "magic",
   "value": "9.5"
  },
  "xmagic": {
   "active": true,
   "type": "xmagic",
   "value": "1.02"
  }
 }
}];

let filteredData = jsonObj.filter(item => item.typeid===56 && item.specialvalue==="0.5");

console.log(filteredData);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • thank you @Rohit; my english is not very good, i'm sorry i can not express myself. The json codes that are working are available on this subject. I am interrogating in php, I am getting other typeid s but I get 56 identity error – Mert Fırat Feb 12 '18 at 11:18
  • If this answer helps you. mark it as correct so that it will be useful for others as well. thanks – Debug Diva Feb 12 '18 at 11:23
  • https://i.hizliresim.com/Pl0LL7.jpg Thank you for your interest, I've tried everything below. it did not work – Mert Fırat Feb 12 '18 at 11:50
  • `===` comparision operator will not work here as it will compare both value as well as type . Just use `==` and try. – Debug Diva Feb 12 '18 at 16:21