I have one array that I want to push a key/value from another array if the array contains a value that the other array have.
var array1 = [
{
"item":"carbon",
"name_i18n": "UI_FUEL_1_NAME_L",
"prefix":"resources",
},
{
"item":"silver",
"name_i18n": "UI_ASTEROID1_NAME_L",
"prefix":"resources",
},
]
I want to check if this array:
var array2 = [
{
"recipe":"RECIPE_ASTEROID1__1",
"ingredients":[
{"item":"silver","qnt":"100"},
{"item":"carbon","qnt":"100"},
]
},
{
"recipe":"RECIPE_FUEL3__2",
"ingredients":[
{"item":"carbon","qnt":"50"},
{"item":"carbon","qnt":"50"},
{"item":"carbon","qnt":"50"},
]
},
Has the value of "item" in array1, if it has, I want to push the recipe key/value into the array1 but without duplicating it, because as you can see the second entry in array2 has 3 ingredients, with the same name, I don't want to put 3x key/value in array1.
Example output after check:
var array1 = [
{
"item":"carbon",
"prefix":"resources",
"recipe":"RECIPE_FUEL3__2",
},
{
"item":"silver",
"prefix":"resources",
"recipe":"RECIPE_ASTEROID1__1",
},
]
I already do a check if they have another property, using this code:
$.each( array1, function( key, value ) {
if (name === value.name_i18n){
name = value.item;
}
});
Which I use to push the value of name into the array2, but it's one value only and not 3 like the example above.