return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Concat two arrays with filtering - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Concat two arrays with filtering - Stack Overflow

programmeradmin1浏览0评论

I have two arrays. I need to bine both of them and make a new array which has dayOfWeek 2, 3, 4, 5, 6. Which means priority for the dayOfWeek is in array1. Means need to keep dayOfWeek 3, 4, 5 from array1.

array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
]

array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

Output should be

finalArray = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

I tried this but it pushes the dayOfWeek from both the arrays. How can I filter them?

const finalArray = []
array1.map((a) => {
    array2.map((a2) => {
        if (a.dayOfWeek === a2.dayOfWeek) {
          finalArray.push(a)
        }
        if (a.dayOfWeek === a2.dayOfWeek) {
          finalArray.push(a2)
        }
    })
})

Thanks in advance!!!

I have two arrays. I need to bine both of them and make a new array which has dayOfWeek 2, 3, 4, 5, 6. Which means priority for the dayOfWeek is in array1. Means need to keep dayOfWeek 3, 4, 5 from array1.

array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
]

array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

Output should be

finalArray = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

I tried this but it pushes the dayOfWeek from both the arrays. How can I filter them?

const finalArray = []
array1.map((a) => {
    array2.map((a2) => {
        if (a.dayOfWeek === a2.dayOfWeek) {
          finalArray.push(a)
        }
        if (a.dayOfWeek === a2.dayOfWeek) {
          finalArray.push(a2)
        }
    })
})

Thanks in advance!!!

Share Improve this question edited Nov 23, 2018 at 7:57 Profer asked Nov 23, 2018 at 7:50 ProferProfer 64310 gold badges47 silver badges97 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 3

You could also simply filter the second array for the items missing form the first and then concatenate those to the first array without lodash:

const a1 = [ {dayOfWeek: 2, home1: "01:30"}, {dayOfWeek: 3, home1: "02:30"}, {dayOfWeek: 4, home1: "03:30"}, {dayOfWeek: 5, home1: "04:30"}, ]
const a2 = [ {dayOfWeek: 3, home1: "05:30"}, {dayOfWeek: 4, home1: "06:30"}, {dayOfWeek: 5, home1: "07:30"}, {dayOfWeek: 6, home1: "08:30"}, ]

const r = a1.concat(a2.filter(x => !a1.some(y => y.dayOfWeek == x.dayOfWeek)))

console.log(r)

This is done via Array.concat, Array.filter and Array.some

Use lodash's _.unionBy(). The predominant array should be the 1st array passed to the function.

const array1 = [{"dayOfWeek":2,"home1":"01:30"},{"dayOfWeek":3,"home1":"02:30"},{"dayOfWeek":4,"home1":"03:30"},{"dayOfWeek":5,"home1":"04:30"}]
const array2 = [{"dayOfWeek":3,"home1":"05:30"},{"dayOfWeek":4,"home1":"06:30"},{"dayOfWeek":5,"home1":"07:30"},{"dayOfWeek":6,"home1":"08:30"}]

const result = _.unionBy(array1, array2, 'dayOfWeek')

console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

If you need to bine several properties to use as the union value, you can use:

_.unionBy(array1, array2, o => `${o.id}-${o.dayOfWeek}`)

You can also use Set and Array.filter

let array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
]

let array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
]

let s = new Set()
console.log([...array1, ...array2].filter(d => {
    let avail = s.has(d.dayOfWeek)
    !avail && s.add(d.dayOfWeek)
    return !avail
  }
) )

you can use array concatenation method and after that do filter.

var c = array1.concat(array2);

Profer. You could use Map object to get unique elements

const array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
];

const array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
];

function getFinalArray(array, uniqueProperty) {
  return array
    .filter(value => value)
    .reduce(
      (arrayMap, item) => {
        return arrayMap.set(item[uniqueProperty], item)
      }, new Map()
    );
}

const result = Array.from(
  getFinalArray([...array2, ...array1], 'dayOfWeek').values()
);

You can use a bination of Array#slice(), Array#forEach() and Array#some() methods:

  • Use .slice(0) to get all elements of array1 in finalArray.
  • Then use .forEach() to iterate over array2 and get all elements that doesn't exist in finalArray.
  • Use .some() to check if the iterated element (dayOfWeek) exist.

This is how should be your code:

var finalArray = array1.slice(0);
array2.forEach(function(a){
      if(!finalArray.some(e => e.dayOfWeek == a.dayOfWeek))
          finalArray.push(a);
});

Demo:

let array1 = [
  {dayOfWeek: 2, home1: "01:30"},
  {dayOfWeek: 3, home1: "02:30"},
  {dayOfWeek: 4, home1: "03:30"},
  {dayOfWeek: 5, home1: "04:30"},
];

let array2 = [
  {dayOfWeek: 3, home1: "05:30"},
  {dayOfWeek: 4, home1: "06:30"},
  {dayOfWeek: 5, home1: "07:30"},
  {dayOfWeek: 6, home1: "08:30"},
];

var finalArray = array1.slice(0);
array2.forEach(function(a){
      if(!finalArray.some(e => e.dayOfWeek == a.dayOfWeek))
          finalArray.push(a);
});

console.log(finalArray);

发布评论

评论列表(0)

  1. 暂无评论