0

I have 2 objects with key-value pairs that should always be identical (match) otherwise I want to modify the value of the key in object #1 to "Some Value - Not available"

Here are my 2 objects:

Object #1

[
  {
    "name": "John",
    "age": "12",
    "id": 1
  },
  {
    "name": "tina",
    "age": "19",
    "id": 2
  }]

Object #2 ( name checker)

 [
      {
        "value": "tina"
      },
      {
        "value": "Trevor"
      },
      {
        "value": "Donald"
      },
      {
        "value": "Elon"
      },
      {
        "value": "Pamela"
      },
      {
        "value": "Chris"
      },
      {
        "value": "Jackson"
      }
    ]

I would like to find out if name in Object #1 is found in Object #2 and if it is not found, add " - not available"

e.i

[
      {
        "name": "John - not available",
        "age": "12",
        "id": 1
      },
      {
        "name": "tina",
        "age": "19",
        "id": 2
      }
]

Important Note: I'm using ES5 --- not ES6

VLAZ
  • 26,331
  • 9
  • 49
  • 67
fosowe
  • 51
  • 8
  • 1
    There seems to be no [JSON objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) in the code ..? – Teemu Jun 15 '20 at 11:25
  • 2
    `Array1.forEach(element => { if(!Array2.some(element2 => element2.value === element.name)){ element.name += " - not available"; } })` –  Jun 15 '20 at 11:25
  • @Teemu I wanted to minimize the question and make it easier to read – fosowe Jun 15 '20 at 11:29
  • @MikeS. thanks! i'm giving that a go – fosowe Jun 15 '20 at 11:29
  • "JS objects" would have been shorter, and correct ... – Teemu Jun 15 '20 at 11:29
  • 1
    @Teemu [nor could there be](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/). [Further information for OP](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – VLAZ Jun 15 '20 at 11:29
  • @Teemu "*"JS objects" would have been shorter, and correct ...*" even shorter: "objects". Still correct. We agree that "JSON objects" is both longer and incorrect. – VLAZ Jun 15 '20 at 11:31
  • @VLAZ But but, I've just linked to the documentation of JSON object, it really exists, but is an intrinsic object in some JS enviroments. – Teemu Jun 15 '20 at 11:32
  • @Teemu semantics, I guess. "JSON object" = 1. object *named* JSON 2. Object that *is* JSON. I was talking about the latter. – VLAZ Jun 15 '20 at 11:33
  • @MikeS. thanks for your solution - however I'm not using ES6 .. I'm using ES5 – fosowe Jun 15 '20 at 11:39
  • @VLAZ I have now added ES5 in the question - for clarification – fosowe Jun 15 '20 at 11:42
  • 1
    @fosowe Just translate the arrow functions in Mike's comment to function expressions then. – Bergi Jun 15 '20 at 11:57

1 Answers1

0

This should work in ES5

object1.forEach(function(person) {
  var found = false;
  Object.keys(object2).forEach(function(key) {
    if (person.name === object2[key].value) {
      found = true;
    }
  });
  if (!found) {
    person.name = person.name + " - not available";
  }
});
PeterSH
  • 425
  • 3
  • 14
  • I don't think `Object.values()` is in ES5. EDIT: Yep, checked and it's added in [ES8](http://www.ecma-international.org/ecma-262/8.0/index.html) – VLAZ Jun 15 '20 at 12:04
  • @PeterSH thanks! note: I'm getting: object1.forEach is not a function... error – fosowe Jun 15 '20 at 12:19
  • @fosowe Check the variable names of your objects, I named them object1 and object2, yours might be different. – PeterSH Jun 15 '20 at 12:38
  • @PeterSH Got it to work.. I had to JSON.parse(JSON.stringify(myObject)) object1 and object2 – fosowe Jun 15 '20 at 13:26