在新对象中组合属性javascript

时间:2014-02-28 20:24:49

标签: javascript javascript-objects

我有一个看起来像这样的Array对象(对象数量大约增加了100倍)

var messages =[
{
        date: "12-12-12",
        time: "12:34:10",
        name: "Wence",
        message: "Howdy matey"
    },
{
        date: "12-12-12",
        time: "12:34:10",
        name: "Alice",
        message: "Everything allright?"
    }
{
        date: "12-12-12",
        time: "12:34:10",
        name: "Wence",
        message: "I'm fine and you?"
    }];

使用此对象,我想创建一个新的keypair结构,如下所示:

{"wence":["Howdy matey","I'm fine and you?"],"Alice":["Everything allright?"]}

2 个答案:

答案 0 :(得分:1)

好的,我将再次使用您新描述的输入和输出数据示例:

var messages = [{
    date: "12-12-12",
    time: "12:34:10",
    name: "Wence",
    message: "Hi Bob"
}, {
    date: "12-12-12",
    time: "12:34:10",
    name: "Bob",
    message: "Howdy partner"
} {
    date: "12-12-12",
    time: "12:34:10",
    name: "Bob",
    message: "Howdy matey."
}];

function structureMessages(list) {
    var data = {}, item;
    for (var i = 0; i < list.length; i++) {
        item = list[i];
        // if no entry for this name yet, initialize it to an empty array
        if (!data.hasOwnProperty(item.name)) {
            data[item.name] = [];
        }
        // add this message to the array for this name
        data[item.name].push(item.message);
    }
    return data;
}


// convert the data format to be messages organized by name
var messagesByName = structureMessages(messages);
console.log(messagesByName["Wence"]);

仅供参考,对于上述数据,这将创建一个如下数据结构:

{Wence: ["Hi Bob"], Bob: ["Howdy partner", "Howdy matey."]}

对象中的每个键都是用户名。每个密钥的数据是该用户的一组消息。如果它是原始数组中的消息对象数组可能会更有用,因为那时你会得到日期和时间信息。您只需更改以下行即可修改上述代码:

 data[item.name].push(item.message);

到此:

 data[item.name].push(item);

如果你这样做,对象中的每个键仍然是用户名,但每个键的数据是一个消息对象数组(其中包含消息,但也包含其他元数据)。

答案 1 :(得分:0)

假设一个数组调用`allMessages',其中包含要搜索的所有消息对象的集合:

var messagesSentByNames = allMessages.reduce(function(relevantMsgs,message) {

  for(var i = 0; i < deelnemers.length; i++) {
    if(message.name == deelnemers[i]) {
      relevantMsgs.push(message);
      break;
    }
  }; 

  return relevantMsgs;
}, []);

如果您想要可重复使用的东西:

/**
 * names: array of Strings or String representing name(s) to search
 * messagesToSearch: array of message objects to find matches in
 */
function getMessagesSentBy(names, messagesToSearch) {
  //Can pass in a single name as string
  if(typeof names === 'string') {
    names = [names];
  }

  return messagesToSearch.reduce(function(relevantMsgs,message) {

    for(var i = 0; i < names.length; i++) {
      if(message.name === names[i]) {
        relevantMsgs.push(message);
        break;
      }
    }; 

    return relevantMsgs;
  }, []);
}

实施例

    var allMessages = [
      {date: "12-12-12",time: "12:34:10",name: "Wence", message: "A"},
      {date: "12-12-12",time: "12:34:10",name: "Bob", message: "B"},
      {date: "12-12-12",time: "12:34:10",name: "Wence", message: "C"},
      {date: "12-12-12",time: "12:34:10",name: "Sanderson", message: "D"}];

    var users = ['Wence','Bob'];

    var wenceMessages = getMessagesSentBy(users, allMessages);

    console.log(wenceMessages);

输出:

[{date: "12-12-12",time: "12:34:10",name: "Wence", message: "A"},
 {date: "12-12-12",time: "12:34:10",name: "Bob", message: "B"},
 {date: "12-12-12",time: "12:34:10",name: "Wence", message: "C"}]