score:-1
use array reduce
obj = [ { type: "", numberofquestions:"", technology:"" }, { type: "1", numberofquestions:"4", technology:"abcd" }, { type: "", numberofquestions:"6", technology:"ass" } ];
obj = obj.find(function(item){ if (!(item.type === '' || item.numberofquestions === '' || item.technology === '')){ return item; } });
console.log('array : ', obj);
score:0
first off, dont use =
in the object please use :
. if you want to check the keys dynamically use this code
const validatedata = (data) => {
data.map((object) => {
object.keys(object).map((innerobj) => {
if(object[innerobj] === "") {
return true;
} else {
return false;
}
})
});
}
var obj = [{ type: "", numberofquestions:"", technology:"" },
{ type: "1", numberofquestions:"4", technology:"abcd" },
{ type: "", numberofquestions:"6", technology:"ass" }];
validatedata(obj);
score:0
you can use filter method
var obj = [ { type: "", numberofquestions:"", technology:"" }, { type: "1", numberofquestions:"4", technology:"abcd" }, { type: "", numberofquestions:"6", technology:"ass" } ]
obj.filter((a)=>{ return a['type'] == "" ? a : a['numberofquestions'] == "" ? a : a['technology'] == "" ? a : '' }).length > 0 ? true : false;
score:0
this will work regardless of key names (using es6 syntax).
var data = [ { type: "", numberofquestions:"", technology:"" }, { type: "1", numberofquestions:"4", technology:"abcd" }, { type: "", numberofquestions:"6", technology:"ass" } ]
const checknull = (data) => data.some(item => object.keys(item).some(key => item[key] == ''));
console.log(checknull(data));
score:3
you can use filter function for this, which return the array on the condition
var container = [ { type: "", numberofquestions:"", technology:"" }, { type: "1", numberofquestions:"4", technology:"abcd" }, { type: "", numberofquestions:"6", technology:"ass" } ]
container.filter((a)=>{ return a['type'] == "" ? a : a['numberofquestions'] == "" ? a : a['technology'] == "" ? a : '' }).length > 0 ? true : false;
score:4
you can use array.prototype.some
var array = [...];
function validatedata (array) {
return array.some(item => item.type === '' || item.numberofquestions === '' || item.technology === '');
}
validatedata(array);
it was es6 solution (with arrow functions).
es5 solution:
function validatedata (array) {
return array.some(function(item) {
return item.type === '' || item.numberofquestions === '' || item.technology === '';
});
}
Source: stackoverflow.com
Related Query
- Find if any item in the array matches the condition
- Compare each item with its next item and find each item big or small by comparing. And push the result to a new array for each
- I have to show an error message whenever a user enters a string that doesn't match any item in the user array
- In React, how do I find the first item in a list that matches some criteria?
- Is there any way to render value on the basis of clicked item from an array in react using router?
- Is there any way to update a specific index from the array in Firestore
- How do I add an item to the front of a state array in React
- What's the advantage of using $splice (from immutability-helper) over filter to remove an item from an array in React?
- Yarn install - Couldn't find any version "react-is" that matches [Latest version]
- Cannot find entry file index.js in any of the roots: [...] (React Native)
- cannot delete array item, the wrong item always gets deleted
- Formik, Material UI Autocomplete and Firestore - where query to find the array parameter
- How to find item in array in React
- how to pass the current item to the function so that i can remove it from array in react component?
- Javascript / React - getting first item in array that matches specific conditions
- React: How to update an item in array without removing all the other props?
- React setState array is appending the same item twice on second call onward
- Array of objects and i want to return true if any key value pair in the object is duplicate
- How to get index of the clicked images which is an item of array using React native
- Need to find the way to slice this array to show only n customers
- When creating buttons for each item in an array in React, how do I pass in unique onClick parameters based on the array value?
- How to change the state of a specific value in an array with condition
- onClick event only leaves the last item of the DOM array instead of removing the target
- Is there any function to use to see the new updated array state using React Js Hook?
- How can i find the length of a nested array objectlist in react?
- How to use Lodash to find a specific item in an array and then use setState() to update it?
- Scroll to the last item of a mapped array
- How can I find a specific item in array in Javascript?
- Javascript - Add Item in the middle of an array inside map function
- How to check for every item on the index 0 of an array if are falsy except for 2?
More Query from same tag
- Formik Field not clearing after form submit
- how to set ref in list of items react js?
- Why does slice push work in redux but not concat?
- build failed due to babelrc, presets[0] [1] must be an object, false or undefined
- Receiving an error when deploying react app to vercel
- How can I 'mock' state value in ReactJS component unittest?
- Can't get components displayed with react-scroll navigation
- more than one setState in useEffect, how to aviod re-render?
- Resolving Promise Array
- Input value is changed after moving to another tab react js hooks
- Required context `router` was not specified. Check the render method of `RoutingContext`
- React state not updating props (using next.js)
- How can I detect refresh page and routes change in react?
- How can I re-trigger a custom hook inside my react component, when its states have not changed?
- React image src passed as props not working
- React: useContext vs variables to store cache
- Fetching API in my react app doesnt work, Unhandled Rejection (TypeError)
- why state is not changing when changing the line of the setstate
- Error when calling theme Props in component using ReactJs and Typescript
- AWS Rekognition JS SDK Invalid image encoding error
- React: Return Statement
- Mock a custom service with jest and enzyme for a react component
- Why after register successfully when first time login blank screen showing, after reload the page then showing the dashboard screen
- In enzyme cannot update state using instance().state and update()
- How can I pass the value of setState on Googlemap api function (initialcenter)?
- React element resets even when its `key` prop is kept the same
- Is it good practice to set CSS variable in the React's render method?
- How to import moment-precise-range-plugin in React.js?
- How to pass a function as an argument to a ReactJS component in TypeScript
- React - state doesn't update in class component even though the message printed on the screen changes