Home > Back-end >  How to filter the aray?
How to filter the aray?

Time:12-06

I want to examine the parts of the array that are objects. What if it checks a value and filters it for me if it exists?

im array :

        [ {description: null
        drive_slots: (4) ['82:17', '82:18', '82:20', '82:22']
        drive_type: "HDD"
        id: 577569},
        {description: null
        drive_slots: (8) ['82:8', '82:9', '82:10', '82:11', '82:12', '82:13', '82:14', '82:15']
        drive_type: "HDD"
        id: 577553
        interface_type: "SAS"},
        {description: null
        drive_slots: (5) ['82:0', '82:1', '82:2', '82:3', '82:4']
        drive_type: "SSD"
        id: 577543
        interface_type: "SATA"}]
 
 if (props.driveData) {
                    props.driveData.map(item => {
                        return drive_slots.push(item)
                    })
                    console.log("???", drive_slots);
                    if (drive_slots) {
                        const drive_filter = drive_slots.filter((drive) => (drive.drive_slots === ('82:17','82:4')));
                        console.log("drive_filter>>>", drive_filter);
                    }
    }

CodePudding user response:

If you want to print something during the search, do not use filter: instead use a forEach loop.

const props = {
  driveData: [{
      description: null,
      drive_slots: ['82:17', '82:18', '82:20', '82:22'],
      drive_type: "HDD",
      id: 577569
    },
    {
      description: null,
      drive_slots: ['82:8', '82:9', '82:10', '82:11', '82:12', '82:13', '82:14', '82:15'],
      drive_type: "HDD",
      id: 577553,
      interface_type: "SAS",
    },
    {
      description: null,
      drive_slots: ['82:0', '82:1', '82:2', '82:3', '82:4'],
      drive_type: "SSD",
      id: 577543,
      interface_type: "SATA",
    }
  ]
}



if (props.driveData) {
  props.driveData.forEach(
    drive => {
      if (drive.drive_slots) {
        drive.drive_slots.forEach(slot => {
          if (['82:17', '82:4'].includes(slot)) {
            console.log("drive_filter>>>", drive);
          }
        })
      }
    }
  )
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

If you want to filter the drives that fit with any of the slots you specify, and print each such drive _once_, then you should indeed use filter

But then you must not print things as you go along, otherwise you may up printing one drive more than once.

The version below read each drive, and identifies whether it contains any of the relevant slots. (It wasn't clear exactly how your original condition was intended to be used.)

Then, after reading all the drives, it has a filtered list of drives meeting the criteria. It then prints that list.

const props = {
  driveData: [{
      description: null,
      drive_slots: ['82:17', '82:18', '82:20', '82:22'],
      drive_type: "HDD",
      id: 577569
    },
    {
      description: null,
      drive_slots: ['82:8', '82:9', '82:10', '82:11', '82:12', '82:13', '82:14', '82:15'],
      drive_type: "HDD",
      id: 577553,
      interface_type: "SAS",
    },
    {
      description: null,
      drive_slots: ['82:0', '82:1', '82:2', '82:3', '82:4'],
      drive_type: "SSD",
      id: 577543,
      interface_type: "SATA",
    }
  ]
}



if (props.driveData) {
  const drivesMeetingCriteria =
    props.driveData.filter(
      drive => drive.drive_slots && (
        drive.drive_slots.filter(
        // See if any of the slots of the drive match any of your search criteria
          slot => ['82:17', '82:4'].includes(slot)).length > 0
      )
    )
  console.log(drivesMeetingCriteria)
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Why you should not use .filter in code that is supposed to print things out

.filter is a clue to the reader of the program that you are only making a new array which has a subset of the elements of a previous array, and not intending to have other side-effects (such as printing or writing to disk).

If you want to loop through an array, but have side-effects as you go, then you should use .forEach because this makes it clear to the reader that you are planning an action during the processing of each item.

Using .filter will work, but you shouldn't do it because

  • It unnecessarily creates a new array, which you then discard

  • It misleads the reader

  • In principle, .filter is parallelizable: you could ask different processes to handle each element; they just have to respond "yes" or "no" to whether the element is to be retained. There is no absolute requirement for them to be processed in the order of the elements in the array. If you have used .filter but require the order of side-effects to be retained, and later somehow parallelise your program, you may have an intermittent bug that will be hard to recognise.

  • Related