2

How can I loop through my json data to find two specific key values e.g. weight = "8m" and meters = "7t" then return the name value of the object where these two values are found e.g. "25t".

data.json (Small sample)

    [
      {
       "name": "20t",
       "weight": ["1t","2t","3t","4t","5t"],
       "meters": ["7m","8m","9m","10m","12m","14m","16m","18m"]
      },
      {
       "name": "25t",
       "weight": ["1t","2t","3t","4t","5t","6t","7t","8t"],
       "meters": ["7m","8m","9m","10m","12m","14m","16m","18m","20m","22m"]
      }
    ]

I'm able to loop through all the data using ngif and ngfor.

    <div *ngIf="crane?.length">
      <div *ngFor="let data of crane">

        <p>{{data.name}}</p>
        <p>{{data.weight}}</p>
        <p>{{data.meters}}</p>

      </div>
    </div>

But I need to output only the specific name value where the weight and meters match the key value I want. Any idea on how I could achieve this? I'm quite new to angular, any help would be appreciated.

1
  • 2
    Can you add output of example? Commented Jan 13, 2019 at 21:21

2 Answers 2

2

Try this:

const data = [
      {
       "name": "20t",
       "weight": ["1t","2t","3t","4t","5t"],
       "meters": ["7m","8m","9m","10m","12m","14m","16m","18m"]
      },
      {
       "name": "25t",
       "weight": ["1t","2t","3t","4t","5t","6t","7t","8t"],
       "meters": ["7m","8m","9m","10m","12m","14m","16m","18m","20m","22m"]
      }
    ]

const w = "7t";
const m = "8m";

const filteredData = data
            .filter(
              (data) => 
                  data.weight
                    .some((weight) => weight === w) 
                  && 
                  data.meters
                    .some((meter) => meter === m)
              );

const keys = filteredData.map((data) => data.name);

console.log(keys);

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

Comments

0

I hanged some of your sentence it did not make a lot of sense compared to the provided data but it looks like this:

const data = [
      {
        "name": "20t",
        "weight": ["1t", "2t", "3t", "4t", "5t"],
        "meters": ["7m", "8m", "9m", "10m", "12m", "14m", "16m", "18m"]
      },
      {
        "name": "25t",
        "weight": ["1t", "2t", "3t", "4t", "5t", "6t", "7t", "8t"],
        "meters": ["7m", "8m", "9m", "10m", "12m", "14m", "16m", "18m", "20m", "22m"]
      }
    ]
    //ow can I loop through my json data to find two specific key values e.g. weight = "8t" and meters = "7m" then return the name value of the object where these two values are found e.g. "25t".

    const result = data.filter(a => a.weight.find(w => w == "8t"))
      .filter(a => a.meters.find(m => m == "7m"))
      .map(a => a.name);
    console.log(result)

1 Comment

This works, however, setting the variables beforehand gives much more flexibility, many thanks

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.