1

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.

Ravi M
  • 71
  • 7
  • You can use `array.includes()` to test: [see also](https://stackoverflow.com/questions/51603456/array-includes-to-find-object-in-array/51603480#51603480) – Mark May 01 '20 at 02:15

3 Answers3

0

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);
Joundill
  • 6,828
  • 12
  • 36
  • 50
0

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

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
0

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'));
rize
  • 1,104
  • 8
  • 9