-3

I have an array named files:

        files = [
            {
                id: "1", 
                title: "aras", 
                price: 100, 
                size: 25, 
                tools: [
                    "Hammer",
                    "Zip",
                    "Line",
                    "Nexti"
                    ]
            },
            {
                id: "2", 
                title: "yasio", 
                price: 150, 
                size: 30, 
                tools: [
                    "Hammer",
                    "Zip",
                    ]
            },
            {
                id: "3", 
                title: "janio", 
                price: 200, 
                size: 30, 
                tools: [
                    "Line",
                    "Nexti"
                    ]
            },
            {
                id: "4", 
                title: "chashio", 
                price: 400, 
                size: 35, 
                tools: [
                    "Nexti"
                    ]
            },



          ]

I want to filter this object, for example, to get the array: prices between 90 to 150 and sizes between 25 to 40 and tools to include Hammer and Zip. output must be:

{id: "1", title: "aras", price: 100, size: 25,tools:[...]},
{id: "2", title: "aras", price: 150, size: 30,tools:[...]},
{id: "4", title: "aras", price: 90,  size: 40,tools:[...]},

How can i do it?

0

2 Answers 2

1
files.filter(function(file){
            return ((file.price>= 90 && file.price<=150) && (file.size >= 25 && file.size <= 40))
    })
Sign up to request clarification or add additional context in comments.

Comments

1

All you need to use Array.prototype.filter() to return items which satisfies the following condition:

(price>=90 && price <= 150) && (size >= 25 && size <= 40)

var files = [
  {id: "1", title: "aras", price: 100, size: 25, tools: ["Hammer","Zip","Line","Nexti"] },
  {id: "2", title: "aras", price: 150, size: 30, tools: ["Hammer","Zip"]},
  {id: "3", title: "aras", price: 160, size: 35, tools: ["Line","Nexti"]},
  {id: "4", title: "aras", price: 90,  size: 40, tools: ["Nexti"]},
  {id: "5", title: "aras", price: 200, size: 45, tools: []}
]

var res = files.filter(f => (f.price>=90 && f.price <= 150) 
                         && (f.size >= 25 && f.size <= 40) 
                         && (f.tools.includes('Hammer') &&  f.tools.includes('Zip')));

console.log(res);

4 Comments

thanks for your answer, my problem is here, I can't filter by tools in my files. do you have some idea for it?
@Aras, filter() is a native JavaScript array method...I don't know why you can't use that!
I use it I want to check some object in my arras list something like includes methods
@Aras, do you want both Hammer and Zip to be present in the tools?....updated the answer with includes.......please check.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.