JavaScript - 基于来自另一个对象

时间:2017-07-17 15:12:15

标签: javascript arrays json node.js

我试图根据传递给API调用的匹配标题从对象数组中获取属性。我有以下两个对象:

第一个对象是一个简单的标题:

{
    "title": "Demo Import - Successful"
}

第二个对象是一个包含上述字段和附加信息的数组:

"data": [{
        "columns": [
            "_source"
        ],
        "sort": [
            "example-timestamp",
            "asc"
        ],
        "title": "Demo Import - Authorization Failure"
    },
    {
        "columns": [
            "m-Form",
            "_type",
            "m-Identity"
        ],
        "sort": [
            "_type",
            "asc"
        ],
        "title": "Demo Import - Timed Out"
    },
    {
        "columns": [
            "_source"
        ],
        "sort": [
            "example-timestamp",
            "asc"
        ],
        "title": "Demo Import - Successful"
    }
]

我目前正试图弄清楚如何找到两个对象之间的匹配属性,然后从以下内容中获取与该标题相关的其余信息:

{
    "title": "Demo Import - Successful",
    "sort": "example-timestamp",
    "direction": "asc",
    "columns": [_source]
}

有人能为我提供一些指导吗?我仍然很擅长使用JavaScript中的对象。我目前的想法是遍历数据,并检查每个索引的匹配标题属性。如果匹配,则存储columns和title属性,然后将排序属性操作为

形式
{
    "title": data.index.title,
    "sort": data.index.sort[0],
    "direction": data.index.sort[1],
    "columns": data.index.columns
}

这是我目前的尝试,我不能完全开始工作:

var match_title = {
    "title": "Demo Import - Successful"
};

var objects =
    "data": [{
            "columns": [
                "_source"
            ],
            "sort": [
                "example-timestamp",
                "asc"
            ],
            "title": "Demo Import - Authorization Failure"
        },
        {
            "columns": [
                "m-Form",
                "_type",
                "m-Identity"
            ],
            "sort": [
                "_type",
                "asc"
            ],
            "title": "Demo Import - Timed Out"
        },
        {
            "columns": [
                "_source"
            ],
            "sort": [
                "example-timestamp",
                "asc"
            ],
            "title": "Demo Import - Successful"
        }
    ];

function getObjects(name, data) {
    for (var i = 0, i < data.length, i++) {
        if (name.title == data[i].title) {
            var response = {
                "title": data[i].title,
                "sort": data[i].sort[0],
                "direction": data[i].sort[1],
                "columns": data[i].columns
            }
            return response;
        };
    };
};

var matchedObject = getObjects(match_title, objects);

提前感谢您的帮助。

编辑:解决了。感谢大家的快速解答和精彩解释!

3 个答案:

答案 0 :(得分:1)

您的objects无效,然后您的代码中出现了很多错误,此处为修复

&#13;
&#13;
var match_title = {
  "title": "Demo Import - Successful"
};

var objects = {
  "data": [{
      "columns": [
        "_source"
      ],
      "sort": [
        "example-timestamp",
        "asc"
      ],
      "title": "Demo Import - Authorization Failure"
    },
    {
      "columns": [
        "m-Form",
        "_type",
        "m-Identity"
      ],
      "sort": [
        "_type",
        "asc"
      ],
      "title": "Demo Import - Timed Out"
    },
    {
      "columns": [
        "_source"
      ],
      "sort": [
        "example-timestamp",
        "asc"
      ],
      "title": "Demo Import - Successful"
    }
  ]
};

function getObjects(name, data) {
  for(var i = 0; i < data.data.length; i++) {
    if(name.title == data.data[i].title) {
      var response = {
        "title": data.data[i].title,
        "sort": data.data[i].sort[0],
        "direction": data.data[i].sort[1],
        "columns": data.data[i].columns
      };
      return response;
    }
  }
}

var matchedObject = getObjects(match_title, objects);
console.log(matchedObject);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

First, your objects JSON is invalid because you are trying to set objects to a property that stores an array, but you didn't wrap the object with { and }. You could just set the variable to a JSON array and then you don't need the property at all (and that's what I'm showing in my answer below).

The Array.filter() method can do this for you without explicit loops.

See below for corrections.

var match_title = { title: "Demo Import - Successful" };

var data = `[
    {
        "columns": [ "_source" ], 
        "sort": [ "example-timestamp", "asc" ],
        "title": "Demo Import - Authorization Failure"
    },
    {
        "columns": [ "m-Form", "_type", "m-Identity" ],
        "sort": [ "_type", "asc" ],
        "title": "Demo Import - Timed Out"
    },
    {
        "columns": [ "_source" ],
        "sort": [ "example-timestamp", "asc" ],
        "title": "Demo Import - Successful"
    }
]`;

// New object will be stored here
var result = null;

function getObjects(name, data) {

  // parse the JSON into an array and filter the array based on input
  var match = JSON.parse(data).filter(function (entry) {

   // Just check if the item being enumerated has the value you seek
   if (entry.title === name){ 
      // Add the new property and set the placeholder object to the result
      entry.direction = entry.sort[1];
      entry.sort.pop();  // Remove sort direction from property array
      result = entry;
   }
  });
};

// Invoke the function
getObjects(match_title.title, data);

// Get the entire matched object:
console.log(result);

// Or, just individual properties of it:
console.log(result.columns);
console.log(result.sort);
console.log(result.title);
console.log(result.direction);

答案 2 :(得分:1)

You could try a combination of Array#filter and Array#map

var match_title = {
  "title": "Demo Import - Successful"
};

var objects = {
  "data": [{
      "columns": [
        "_source"
      ],
      "sort": [
        "example-timestamp",
        "asc"
      ],
      "title": "Demo Import - Authorization Failure"
    },
    {
      "columns": [
        "m-Form",
        "_type",
        "m-Identity"
      ],
      "sort": [
        "_type",
        "asc"
      ],
      "title": "Demo Import - Timed Out"
    },
    {
      "columns": [
        "_source"
      ],
      "sort": [
        "example-timestamp",
        "asc"
      ],
      "title": "Demo Import - Successful"
    }
  ]
};

function getObjects(name, data) {
  return data.filter(function(d) {
    return name.title == d.title;
  }).map(function(d) {
    return {
      "title": d.title,
      "sort": d.sort[0],
      "direction": d.sort[1],
      "columns": d.columns
    }
  });
};

var matchedObject = getObjects(match_title, objects.data);
console.log(matchedObject)