1

i have bellow two object arrays

var array_1 = [{"Date": "2020-04-01", "Item" : 001},{"Date": "2020-04-03", "Item" : 002}]
var base_array = [{"Date": "2020-04-01", "Item" : null}, {"Date": "2020-04-02", "Item" : null}, 
                  {"Date": "2020-04-04", "Item" : null}]

i planning modify array_1 as bellow

 var array_1 = [{"Date": "2020-04-01", "Item" : 001},
                {"Date": "2020-04-02", "Item" : null},
                {"Date": "2020-04-03", "Item" : 002},
                {"Date": "2020-04-04", "Item" : null}]

since date "2020-04-02" and "2020-04-04" not exist in array_1, those dates should be push with "item" null and "2020-04-01" should not be push since its already exist in array_1.

i've tried make following each loop but not able to continue

small note : base_array will always contain more value than array_1. so that why i used base_array as my initial loop

$.each(base_array , function (key,bvalue) {

    $.each(array_1, function (key,value) {
          if(bvalue.Date != value.Date){

               array_1.push({"Date" : value.Date, "Item": value.Item})
          }
    })
})

5 Answers 5

2

You can use Array.reduce() on base_array, passing array_1 as the initial value and only adding values from base_array where the Date does not already exist in the carry value:

var array_1 = [{
  "Date": "2020-04-01",
  "Item": 001
}, {
  "Date": "2020-04-03",
  "Item": 002
}];
var base_array = [{
    "Date": "2020-04-01",
    "Item": null
  }, {
    "Date": "2020-04-02",
    "Item": null
  },
  {
    "Date": "2020-04-04",
    "Item": null
  }
];

array_1 = base_array.reduce((c, v) => c.concat(c.some(e => e.Date == v.Date) ? [] : [v]), array_1);
console.log(array_1);

Sign up to request clarification or add additional context in comments.

Comments

2

You could take a hash table, assign the object to the same date and get an array of objects.

At the end sort the array by Date.

var array_1 = [{ Date: "2020-04-01", Item: "001" }, { Date: "2020-04-03", Item: "002" }],
    base_array = [{ Date: "2020-04-01", Item: null }, { Date: "2020-04-02", Item: null }, { Date: "2020-04-04", Item: null }],
    result = Object
        .values([...base_array, ...array_1].reduce((r, o) => (r[o.Date] = o, r), {}))
        .sort(({ Date: a }, { Date: b }) => a > b || -(a < b))

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

1

I'm not sure if the order mattered to you, but if not here's one solution

// Keep track of all dates already present in `array_1`.
let array_1_dates = new Set(array_1.map(array_1_elem => array_1_elem.Date));

// Push elements from `base_array` that are not in `array_1`.
for (let base_array_elem of base_array) {
  if (!array_1_dates.has(base_array_elem.Date)) {
    array_1.push(base_array_elem);
  }
}

Comments

1

First remove from base_array the itens that already are on array_1, using filter. Then concat.

var array_1 = [{"Date": "2020-04-01", "Item" : 001},{"Date": "2020-04-03", "Item" : 002}]
var base_array = [{"Date": "2020-04-01", "Item" : null}, {"Date": "2020-04-02", "Item" : null}, {"Date": "2020-04-04", "Item" : null}]
                  
var filtered = base_array.filter(({Date}) => !array_1.some(el => el.Date == Date));
var result = array_1.concat(filtered);
console.log(result);

Comments

1

You can use a simple loop and find the insert position using comparison.

var array_1 = [{
  "Date": "2020-04-01",
  "Item": 001
}, {
  "Date": "2020-04-03",
  "Item": 002
}]
var base_array = [{
    "Date": "2020-04-01",
    "Item": null
  }, {
    "Date": "2020-04-02",
    "Item": null
  },
  {
    "Date": "2020-04-04",
    "Item": null
  }
]

// iterate over second array
base_array.forEach(el => {
  for (let i = 0; i < array_1.length; i++) {
    // if value found return
    if (array_1[i].Date === el.Date) return;
    // if position found, insert and return
    if (array_1[i].Date > el.Date) {
      array_1.splice(i, 0, el);
      return
    }
  }
  // or insert at the end of the array
  array_1.push(el);
})

console.log(array_1);

NOTE : Assuming the first array is always in sorted order.


UPDATE : Or if order may change and you don't want to add them at the end then you can use Array#some and Array#filter method.

var array_1 = [{
  "Date": "2020-04-01",
  "Item": 001
}, {
  "Date": "2020-04-03",
  "Item": 002
}]
var base_array = [{
    "Date": "2020-04-01",
    "Item": null
  }, {
    "Date": "2020-04-02",
    "Item": null
  },
  {
    "Date": "2020-04-04",
    "Item": null
  }
]

array_1.push(...base_array.filter(o => array_1.every(o1 => o1.Date !== o.Date)))

console.log(array_1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.