最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - need add object to array if name not exist (js) - Stack Overflow

programmeradmin5浏览0评论

Trying add object to array if name not exist . but have dublicates. For example array: [{day:"mo"},{day:"tu"},{day:"st"}].

need to add {day:we} if it not in array. so first need to check if {day:we} not in array then add it.

result: [{day:"mo"},{day:"tu"},{day:"we"},{day:"st"}].

for (name in result) {
            if(name !== 'mo') {
              result.push({day:'mo'});
            }
            if(name !== 'tu') {
              result.push({day:'tu'});
            }
            if(name !== 'we') {
              result.push({day:'we'});
            }
            if(name !== 'th') {
              result.push({day:'th'});
            }
            if(name !== 'fr') {
              result.push({day:'fr'});
            }
            if(name !== 'sa') {
              result.push({day:'sa'});
            }
            if(name !== 'su') {
              result.push({day:'su'});
            }
              break;
          }

Trying add object to array if name not exist . but have dublicates. For example array: [{day:"mo"},{day:"tu"},{day:"st"}].

need to add {day:we} if it not in array. so first need to check if {day:we} not in array then add it.

result: [{day:"mo"},{day:"tu"},{day:"we"},{day:"st"}].

for (name in result) {
            if(name !== 'mo') {
              result.push({day:'mo'});
            }
            if(name !== 'tu') {
              result.push({day:'tu'});
            }
            if(name !== 'we') {
              result.push({day:'we'});
            }
            if(name !== 'th') {
              result.push({day:'th'});
            }
            if(name !== 'fr') {
              result.push({day:'fr'});
            }
            if(name !== 'sa') {
              result.push({day:'sa'});
            }
            if(name !== 'su') {
              result.push({day:'su'});
            }
              break;
          }
Share Improve this question edited Aug 24, 2017 at 15:16 Егор Кротенко asked Aug 24, 2017 at 15:10 Егор КротенкоЕгор Кротенко 8183 gold badges14 silver badges35 bronze badges 3
  • please add the misisng variables and their content -- and the wanted result as well. – Nina Scholz Commented Aug 24, 2017 at 15:12
  • @NinaScholz I want in result add object to array if it not exist in array – Егор Кротенко Commented Aug 24, 2017 at 15:15
  • updated, please check – Егор Кротенко Commented Aug 24, 2017 at 15:19
Add a ment  | 

3 Answers 3

Reset to default 3

You may filter an array of days and check if they exist in result already:

["mo","tu","we","th","fr","sa","su"]
  .filter( day => ! result.some(obj => obj.day === day))
  .forEach( day => {
    result.push({day});
  });

More performant version:

var days = new Set( ["mo","tu","we","th","fr","sa","su"]);

result.forEach( day => days.delete(day.day));

days.forEach(day => result.push({day}));

let days = [{day:"mo"},{day:"tu"},{day:"st"}]
let toAdd = ["mo","tu","wed","thu","fri"]

toAdd.forEach( day1 => {
	if(!days.find( day2 => day2===day1 )) days.push({day:day1})
})

console.log(days)

You could check if the right order of the days is given and insert before a greater day number.

function add(array, insert) {
    var days = { mo: 0, tu: 1, we: 2, th: 3, fr: 4, sa: 5, su: 6 };

    array.some(function (a, i, aa) {
        if (days[a.day] === days[insert.day]) {
            return true;
        }
        if (days[insert.day] < days[a.day]) {
            aa.splice(i, 0, insert);
            return true;
        }
    }) || array.push(insert);
}

var array = [{ day: "tu" }, { day: "sa" }];

add(array, { day: "tu" }); // no insert
console.log(array);

add(array, { day: "mo" }); // insert at the beginning
console.log(array);

add(array, { day: "we" }); // insert in the middle
console.log(array);

add(array, { day: "su" }); // insert at the end
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

发布评论

评论列表(0)

  1. 暂无评论