0

I am new into JavaScript and I have few questions please. First, I am going through two objects to compute the difference between them. So, I have my loop and bunch of if statement for testing. What I am doing is if I find the element I am looking for, I need to add it to a new object while my object is a key and value. The format is like this: obj={programValue[i] : deviceValue[i]} I do it like that because I want this format : obj={'p1': 'app1'}

This result an error, how can I add an element in my object?

My second question is how can I go through an objects that contains other objects itself and test the existence of a key or a value of another objects that contains objects as well. Here is an example to explain: var obj1={{'p1':'app1'}, {'p2':'app2'}};

var obj2= {{'p1':'app2'}, {'p1':'app1'}}

So I want to test if app2 from obj1 exists in obj2, if so then test the key too. What I need is that they have some value (app) but different keys (p1 or p2). I tried many things but didn't fulfill what I need.

Thank you,

  • `{{'p1':'app2'}, {'p1':'app1'}}` is not a valid javascript object – Dom May 31 '18 at 22:10
  • If you are trying to use a dynamic property name during initialization you need to use the correct [syntax for computed property names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names) if your environment supports it – Patrick Evans May 31 '18 at 22:12
  • Ok thank you @PatrickEvans , will do that. – Amira Said May 31 '18 at 22:23
  • @Dom why? can't we have object of objects? – Amira Said May 31 '18 at 22:25
  • Sure you can. But objects are consist of `key: value` pairs. `{{'p1':'app2'}}` is missing the key of the outer object. Maybe you want an array of objects instead? Either way, please ask only one question per question. – Felix Kling May 31 '18 at 22:38
  • @FelixKling I will recheck my results and maybe I can change it to an array – Amira Said May 31 '18 at 22:47

3 Answers3

0

use es6 computed properties

var obj={[programValue[i]] : deviceValue[i]}

or with es5

var obj={};
obj[programValue[i]] = deviceValue[i];

Also for 2nd question:

Object.entries(obj1).some(([key1, value1])=>Object.entries(obj2).some(([key2, value2])=>key1===key2 && value1===value2))
Shishir Arora
  • 5,521
  • 4
  • 30
  • 35
  • I am using es5, but when I try that method, it only adds one element. Since I am putting it in a loop so it can iterate and add element into my object. Do you know why is that? Thank you! – Amira Said Jun 01 '18 at 19:27
0

For question 1:

The format is like this: obj={programValue[i] : deviceValue[i]} I do it like that because I want this format : obj={'p1': 'app1'}, This result an error, how can I add an element in my object?

If using ES6, you can do it like this let obj = {[programValue[i]]:deviceValue[i]};

If programValue[i] returns 'p1' and deviceValue[i] returns 'app1' then your obj will be {'p1':'app1'}

If you are not using ES6, then you will have to first declare empty object and then set your properties

var obj = {}; obj[[programValue[i]] = deviceValue[i];

For your second question, var obj2= {{'p1':'app2'}, {'p1':'app1'}}

That structure wont work. It has to be an array of objects. Then you can iterate through your array or directly access the element using index.

var obj2 =[{'p1':'app1'}, {'p2':'app2'}]

obj2[0].p1 // app1

if you still need an object, then you need a property for each of the object

var obj2 = {one: {'p1':'app1}, two: {'p2':'app2}}

obj2.one.p1 //app1

Karthikeyan
  • 302
  • 1
  • 11
0

var obj2 = {{'p1':'app2'}, {'p1':'app1'}} is not a valid syntax. The correct object syntax would be var obj2 = {'p1':'app2', 'p2':'app1'}.

To add an element to an object you can do object2['someUniqueId'] = 'whatever you like' or object2.someUniqueId = 'whatever you like'

I believe you have to turn both obj1 and obj2 into an Array: var obj1 = [{'p1':'app1'}, {'p2':'app2'}]; var obj2 = [{'p1':'app1'}, {'p2':'app2'}]; and then you could try to check if an object is in your Array obj2 with this example How to determine if object is in array

eltongonc
  • 97
  • 9
  • When I try object2.programValue[i] = deviceValue[i] it doesn't work and says cannot read property '0' undefined coz my i = 0 on that time – Amira Said Jun 01 '18 at 19:32
  • if i = 0 you have to make sure that `object2.programValue[i]` and `deviceValue[i]` are both Arrays. Both have to be constructed as follows `var deviceValue = ["value", "value2"];` and `object2.programValue = []` to get deviceValue[0] wich equals to "value" and placing it into programValue[0] – eltongonc Jun 03 '18 at 19:04