0

Let's say you have JSON like the following:

[{
    "Rarity": "Common",
    "EffectOne": "+4 ACC",
    "EffectTwo": "-1 SPD",
    "EffectThree": "",
  },
{
    "Rarity": "Common",
    "EffectOne": "+4 ACC",
    "EffectTwo": "-1 SPD",
    "EffectThree": "",
  },
{
    "Rarity": "Rare",
    "EffectOne": "+4 ACC",
    "EffectTwo": "-1 SPD",
    "EffectThree": "",
  }]

How can you target the objects that have a shared value for one of the Properties, such as "Rarity": "Common".

Can you do (for the sake of this example, let's say this JSON is just named data): data.rarity.common? or would it be data.rarity("Common"). The reason I ask is I actually can't seem to find this info anywhere in JSON jquery documentation and I would like to populate data in different places based on specific values.

7
  • 2
    You have some extra commas in your JSON. You're likely not finding the information you need in jQuery's documentation because this primarily done without the need for libraries or frameworks - objects, arrays, properties, etc, are all fundamentals of plain JS. If you wanted to narrow down your array to only objects that have "Rarity": "Common", you have a few options, primarily data = data.filter(x => x.Rarity == "Common"); An Example Commented Jul 18, 2017 at 18:12
  • Boom, thanks so much! I couldn't seem to find this info. Much Appreciated! Commented Jul 18, 2017 at 18:14
  • 2
    Not a problem. Added an example to demonstrate. Also, just to be clear, the reason you cannot do data. is because data is an array. It does not have properties, but rather indexes. The filter function I've provided simply iterates through each item (as x), and evaluates the condition supplied (x.Rarity == "Common"). If the result is false, the item is removed from the array. Commented Jul 18, 2017 at 18:15
  • You're a god send man! Enjoy your day. Super helpful. Commented Jul 18, 2017 at 18:21
  • 2
    Possible duplicate of javascript filter array of objects Commented Jul 18, 2017 at 18:45

1 Answer 1

3

There you go with plain JavaScript. The variable common_value is set to Common as a filter for the rarity. Is this what you are looking for?

var json = [{
    "Rarity": "Common",
    "EffectOne": "+4 ACC",
    "EffectTwo": "-1 SPD",
    "EffectThree": "",
  },
{
    "Rarity": "Common",
    "EffectOne": "+4 ACC",
    "EffectTwo": "-1 SPD",
    "EffectThree": "",
  },
{
    "Rarity": "Rare",
    "EffectOne": "+4 ACC",
    "EffectTwo": "-1 SPD",
    "EffectThree": "",
  }];

var common_value = "Common";
var results = [];

for(var i = 0; i < json.length; i++){
	if(json[i].Rarity === common_value){
  	results.push(json[i]);
  }
}

console.log(results);

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

1 Comment

Your're welcome. Remember you can change the ".Rarity" to any key you like for controlling the filter :-)

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.