将2个对象数组相互映射但不同的长度

时间:2017-04-25 08:35:07

标签: javascript dictionary

arr1 = [{
  date: "2017-4-01"
}, {
  date: "2017-4-02"
}, {
  date: "2017-4-03"
}, {
  date: "2017-4-04"
}]

arr2 = [{
  date: "2017-4-01",
  company: "apple"
}, {
  date: "2017-4-04",
  company: "google"
}]

我无法将arr1和arr2映射到一起。如何将Arr2的现有属性键分配给Arr1?输出的长度应为Arr1。

预期结果

arr3 = [{
  date: "2017-4-01",
  company: "apple"
}, {
  date: "2017-4-02"
}, {
  date: "2017-4-03"
}, {
  date: "2017-4-04",
  company: "google"
}]

arr2可能就像这样

const arr2 = [{
  date: "2017-4-01",
  company: "apple"
}, {
  date: "2017-4-04",
  company: "google"
},
{
  date: "2017-4-04",
  company: "tesla"
}
];

表示它可能有多个相同的日期,如果是这种情况,公司应该是数组而不是字符串。

3 个答案:

答案 0 :(得分:1)

您可以将Map用于arr2,并使用Object.assign获取结果的任意属性数。

var arr1 = [{ date: "2017-4-01" }, { date: "2017-4-02" }, { date: "2017-4-03" }, { date: "2017-4-04" }],
    arr2 = [{ date: "2017-4-01", company: "apple", foo: 24 }, { date: "2017-4-04", company: "google" }],
    map = new Map(arr2.map(o => [o.date, o])),
    result = arr1.map(o => Object.assign({}, o, map.get(o.date) || {}));

    console.log(result);

答案 1 :(得分:0)

您可以使用map()find()来匹配与其他数组相同日期的对象。

var arr1 = [{date: "2017-4-01"}, {date: "2017-4-02"}, {date: "2017-4-03"}, {date: "2017-4-04"}]
var arr2 = [{date: "2017-4-01",company: "apple"}, {date: "2017-4-04",company: "google"}]

var result = arr1.map(function(e) {
  var f = arr2.find(a => a.date == e.date);
  return f ? e = Object.assign({}, e, f) : e;
})

console.log(JSON.stringify(result, 0, 4))

另一种方法是将一个对象创建为哈希表,并使用它来检查日期。

var arr1 = [{date: "2017-4-01"}, {date: "2017-4-02"}, {date: "2017-4-03"}, {date: "2017-4-04"}]
var arr2 = [{date: "2017-4-01",company: "apple"}, {date: "2017-4-04",company: "google"}]

var hash = {}
arr2.forEach(e => hash[e.date] = e);

var result = arr1.map(function(e) {
  return hash[e.date] ? e = Object.assign({}, e, hash[e.date]) : e;
})

console.log(JSON.stringify(result, 0, 4))

更新:如果对象日期重复多次,则将数组创建为属性值。

var arr1 = [{"date":"2017-04-01"},{"date":"2017-04-02"},{"date":"2017-04-03"},{"date":"2017-04-04"},{"date":"2017-04-05"},{"date":"2017-04-06"},{"date":"2017-04-07"},{"date":"2017-04-08"},{"date":"2017-04-09"},{"date":"2017-04-10"},{"date":"2017-04-11"},{"date":"2017-04-12"},{"date":"2017-04-13"},{"date":"2017-04-14"},{"date":"2017-04-15"},{"date":"2017-04-16"},{"date":"2017-04-17"},{"date":"2017-04-18"},{"date":"2017-04-19"},{"date":"2017-04-20"},{"date":"2017-04-21"},{"date":"2017-04-22"},{"date":"2017-04-23"},{"date":"2017-04-24"},{"date":"2017-04-25"},{"date":"2017-04-26"},{"date":"2017-04-27"},{"date":"2017-04-28"},{"date":"2017-04-29"},{"date":"2017-04-30"}]
var arr2 = [{"date":"2017-04-23","company_id":"123","company_name":"Pancong Bro"},{"date":"2017-04-23","company_id":"345","company_name":"Le Meridien - Kuala Lumpur"},{"date":"2017-04-18","company_id":"567","company_name":"Three Little Birds Coffee"},{"date":"2017-04-18","company_id":"778","company_name":"Await Cafe"},{"date":"2017-04-18","company_id":"234","company_name":"TOKB CAFE"},{"date":"2017-04-14","company_id":"190","company_name":"Sakae Sushi - Sunway Pyrammid"},{"date":"2017-04-23","company_id":"008","company_name":"sold out Bangsar South"},{"date":"2017-04-19","company_id":"255","company_name":"Greyhound Cafe"}]


var hash = {}
arr2.forEach(function(e) {
  if (!hash[e.date]) {
    hash[e.date] = e
  } else {
    Object.keys(e).forEach(function(key) {
      var prop = hash[e.date][key]
      if (prop && key != 'date') {
        if (typeof prop == 'string') {
          hash[e.date][key] = [prop].concat(e[key])
        } else {
          prop.push(e[key])
        }
      } else {
        prop = e[key];
      }
    })
  }
});

var result = arr1.map(function(e) {
  return hash[e.date] ? e = Object.assign({}, e, hash[e.date]) : e;
})

console.log(JSON.stringify(result, 0, 4))

答案 2 :(得分:0)

您可以使用filter()map()的组合。

如果您有其他属性,则只需更改if (filteredArray.length > 0)的正文。

arr2不需要排序



const arr1 = [{date: "2017-4-01"}, {date: "2017-4-02" }, {date: "2017-4-03"=}, {date: "2017-4-04"}];

const arr2 = [{date: "2017-4-01", company: "apple"}, {date: "2017-4-04",company: "google"}, {date: "2017-4-04",company: "tesla" }];

const arr3 = arr1.map(el => {
  const filteredArray = arr2.filter(e => e.date === el.date)
  if (filteredArray.length > 0) {
    let company;
    if (filteredArray.length === 1) {
        company = filteredArray[0].company
    } else {
        company = filteredArray.map(e => e.company);
    }
    return {date: el.date, company: company};
  }
  
  return el;
})

console.log(arr3)