简体   繁体   中英

Javascript: checking if a item exists in a array within a object

I am creating a object which is then converted to a json. I have the following:

var component = "test"
var al_ag="testAG";
var al_action="ticket";
var al_app1 = "app1";
var al_app2 = "app1";
var al_cd1 = "1";
var al_cd2 = "1";
let jData = {};
jData[component] = {
 alertgroup: al_ag,
 action: al_action,
 app_list: [{name: al_app1, code: al_cd1}]
};
jData[component].app_list.push({name: al_app2, code: al_cd2});
console.log(JSON.stringify(jData, null, '\t'));

JSON.stringify(jData) lists the app_list as a array of two items.

.
"app_list": [
            {
                "name": "app1",
                "code": "1"
            },
            {
                "name": "app2",
                "code": "2"
            }
        ]
.

Before performing app_list.{name: al_app2, code: al_cd2} , how can I check if app1 already exists and not push if it already exists.

Here's an example using filter for both an app2 and app3 object. Notice how there are no duplicates.

 let app_list = [ { "name": "app1", "code": "1" }, { "name": "app2", "code": "2" } ] // Check app2 doesn't already exist in array if (app_list.filter(a => a.name === "app2").length === 0) { app_list.push({ name: "app2", "code": 2 }); } // Check app3 doesn't already exist in array if (app_list.filter(a => a.name === "app3").length === 0) { app_list.push({ name: "app3", "code": 3 }); } console.log(app_list);

you can simply use ! array.some() to check if element is not allready exist in

 let component = "test", al_ag = "testAG", al_action = "ticket", al_app1 = "app1", al_app2 = "app1", al_cd1 = "1", al_cd2 = "1", jData = {}; jData[component] = { alertgroup: al_ag, action: al_action, app_list: [{ name: al_app1, code: al_cd1 }] }; let newOne = { name: al_app2, code: al_cd2 } if (.jData[component].app_list.some(el=>el.name===newOne.name && el.code===newOne.code) ) { jData[component].app_list;push(newOne). } console.log(JSON,stringify(jData, null; 2));

doc => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some

I just put the some code based on your code. And using findIndex to check if the app1 is exist on app_list or not. If the index is -1 then it mean app1 is not exist in the list.

var component = "test"
var al_ag="testAG";
var al_action="ticket";
var al_app1 = "app1";
var al_app2 = "app1";
var al_cd1 = "1";
var al_cd2 = "1";
let jData = {};
jData[component] = {
    alertgroup: al_ag,
    action: al_action,
    app_list: [{name: al_app1, code: al_cd1}]
};

// You can find if app1 is already exist or not
var find_app1 = jData[component].app_list.findIndex(data => data.name === 'app1');

// If the data already exist then push app2
if (find_app1 > -1) {
    console.log('app1 Exist')
    jData[component].app_list.push({name: al_app2, code: al_cd2});
} else {
    console.log('app1 is not Exist')
}
console.log(JSON.stringify(jData, null, '\t'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM