1

i want to search in array to find something

this is my array in this.state

materialUnits: [
                    {
                        unitID: '',
                        barcodeNo: '',
                        salePrice: '',
                        vatValue: '',
                        isdefault: '',
                        rate: ''
                    }
                ]

then in if condition something like this

const value = "1";
const searcharray = this.state.materialUnits.filter(data => {
  return data.isdefault === value;
});

if (searcharray.length === 0) {
  //do something
} else {
  alert("cant 2 defaults");
}
2
  • 3
    How about using Array.prototype.find or Array.prototype.findIndex? Commented Jun 18, 2019 at 12:11
  • Just FYI, that is an object inside of an array (an array with one item) Commented Jun 18, 2019 at 20:38

4 Answers 4

3
return materialUnits.find(item => item.isdefault !== 1);
Sign up to request clarification or add additional context in comments.

Comments

2
return !materialUnits.filter(item => item.isdefault == 1).length > 0;

1 Comment

I would recommend not using the double equal == but rather the triple equal operator ===.
1

var materialUnits =  [
	{
		unitID: '1',
		barcodeNo: '1',
		salePrice: '1',
		vatValue: '1',
		isdefault: '1',
		rate: '1'
	},
	{
		unitID: '2',
		barcodeNo: '2',
		salePrice: '2',
		vatValue: '2',
		isdefault: '2',
		rate: '2'
	}
];

function filterData(data){
  return data.isdefault === '1' ? false: true;
}

console.log(materialUnits.filter(filterData));

Comments

1

function Test (){
    const [materialUnits,setmaterialUnits] = React.useState([
      {
          unitID: '',
          barcodeNo: '',
          salePrice: '',
          vatValue: '',
          isdefault: '1',
          rate: ''
      },
                 {
          unitID: '',
          barcodeNo: '',
          salePrice: '',
          vatValue: '',
          isdefault: '2',
          rate: ''
      },
  ]);
  const [arrayfilter,setfiltearray] =  React.useState(materialUnits);
  
  // call function on input change to filter the array
 
  function filterarray(value){
if(value!==""){
  var newarray =  materialUnits.filter(data=>{return(data.isdefault === value)});
  console.log(newarray)
  setfiltearray(newarray)
}
else{
  setfiltearray(materialUnits)
}

  }

//conditional render depending on  array filtration

  function renderUnit(){
    if(arrayfilter.length===0){
      return(<div>Not found</div>)
    }
    else{
      var units = arrayfilter.map((data,key)=>{return(<div key={key}>{data.isdefault}</div>)})
      return units;
    }
  }
  return(
    <div>
<input type ="text" onChange={e=>filterarray(e.target.value)}/>
{renderUnit()}
</div>
  )
  }

2 Comments

how to use it like if it is not exist then do something if yes alert
well this similar for what u trying to do here i used react hooks !

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.