I have One List where we have some data and flags, I need to update those flags based on another array that contains Ids, if any id is not found in the List then add this Id in list with some default values. My list and arrays are :
var objBaseList =[{Id :1,:Value:"Name1",Status:false,IsDeleted:false},
{Id :2,:Value:"Name2",Status:false,IsDeleted:false},
{Id :3,:Value:"Name3",Status:false,IsDeleted:false},
{Id :4,:Value:"Name4",Status:false,IsDeleted:false},
{Id :5,:Value:"Name5",Status:false,IsDeleted:false},
{Id :6,:Value:"Name6",Status:false,IsDeleted:false},
{Id :7,:Value:"Name7",Status:false,IsDeleted:false},
{Id :8,:Value:"Name8",Status:false,IsDeleted:false},
{Id :9,:Value:"Name9",Status:false,IsDeleted:false},
]
var listIds = [2,6,7];
I need to do it in Jquery. Any Idea what approach should i follow
CodePudding user response:
Below is your code to find a related value of your object
var objBaseList =[{Id :1,Value:"Name1",Status:false,IsDeleted:false},
{Id :2,Value:"Name2",Status:false,IsDeleted:false},
{Id :3,Value:"Name3",Status:false,IsDeleted:false},
{Id :4,Value:"Name4",Status:false,IsDeleted:false},
{Id :5,Value:"Name5",Status:false,IsDeleted:false},
{Id :6,Value:"Name6",Status:false,IsDeleted:false},
{Id :7,Value:"Name7",Status:false,IsDeleted:false},
{Id :8,Value:"Name8",Status:false,IsDeleted:false},
{Id :9,Value:"Name9",Status:false,IsDeleted:false},
]
var listIds = [2,6,7];
for(var i=0; i<listIds.length; i )
{
if (objBaseList.findIndex(item => item.Id == listIds[i]) == -1) {
console.log('Not found');
} else {
console.log('Found :' listIds[i])
}
}
now based on condition you can set the value as per your requirements.