0

I have JavaScript object array with the following structure:

const fruits =       [{
                        "id": "1",
                        "title": "Banana",
                        "description": "1 Minute"
                    },
                    {
                        "id": "2",
                        "title": "Apple",
                        "description": "2 - 3 Days"
                    },
                    {
                        "id": "3",
                        "title": "Manggo",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "4",
                        "title": "Orange",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "5",
                        "title": "Grape blue",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "6",
                        "title": "Grape red",
                        "description": "10 - 20 Days"
                    }]

I want to extract object which has description = "10 - 20 Days" and title not contains "Grape"

Then my expected should be

                    {
                        "id": "3",
                        "title": "Manggo",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "4",
                        "title": "Orange",
                        "description": "10 - 20 Days"
                    }
3

2 Answers 2

1

This is very simple to do by using Array.protoype.filter. Within the filter callback, you can use String.protoype.includes method to check if the title includes "grapes":

const fruits = [{
    "id": "1",
    "title": "Banana",
    "description": "1 Minute"
  },
  {
    "id": "2",
    "title": "Apple",
    "description": "2 - 3 Days"
  },
  {
    "id": "3",
    "title": "Manggo",
    "description": "10 - 20 Days"
  },
  {
    "id": "4",
    "title": "Orange",
    "description": "10 - 20 Days"
  },
  {
    "id": "5",
    "title": "Grape blue",
    "description": "10 - 20 Days"
  },
  {
    "id": "6",
    "title": "Grape red",
    "description": "10 - 20 Days"
  }
];

const filteredFruits = fruits.filter(fruit => {
  if (!fruit.title.toLowerCase().includes("grape")) {
    if (fruit.description === "10 - 20 Days") {
      return true;
    }
  }

  return false;
});

console.dir(filteredFruits)

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

Comments

0

you should use filter like this

const fruits =       [{
                        "id": "1",
                        "title": "Banana",
                        "description": "1 Minute"
                    },
                    {
                        "id": "2",
                        "title": "Apple",
                        "description": "2 - 3 Days"
                    },
                    {
                        "id": "3",
                        "title": "Manggo",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "4",
                        "title": "Orange",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "5",
                        "title": "Grape blue",
                        "description": "10 - 20 Days"
                    },
                    {
                        "id": "6",
                        "title": "Grape red",
                        "description": "10 - 20 Days"
                    }]

const res = fruits.filter(val=> !val.title.includes('Grape') && val.description.includes('10 - 20 Days'))

console.log(res)

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.