使用Array中的值创建对象

时间:2016-11-30 22:07:41

标签: javascript jquery

我有一个包含唯一列名列表的对象:

["heading", "startDate", "finishDate"]

我正在返回一个类似于此的XML数据集:

<z:row ows_heading='Header' ows_startDate='1/1/2016' ows_finishDate='1/11/2016' ows_Description='Ignore me'/>
<z:row ows_heading='Header' ows_startDate='2/3/2016' ows_finishDate='2/12/2016' ows_Description='Ignore me'/>

如何遍历唯一列名对象以获取列名,附加&#34; ows _&#34;然后找到结果值并将其添加回对象,以便最终结果如下:

["heading": "Header", "startDate": "1/1/2016", "finishDate": "1/11/2016"]

编辑:当前代码块:

var a=[];var obj={};
        $(r).find("[nodeName=z:row]").each(function() 
        {
            $.each(uniqHeaderVals, function( key, value ) {
                var thisVal = $(this).attr("ows_"+value);
                obj.value = thisVal;
            });

            a.push(obj);
        });
        console.log(a);

4 个答案:

答案 0 :(得分:1)

在@ScottMarcus的基础上建立几乎正确的答案

&#13;
&#13;
var arry = ["heading", "startDate", "finishDate"];

var elems = document.getElementsByTagName("z:row");

var resultArry = [].map.call(elems, function(elem) {
  // For each element, create an object
  return arry.reduce(function(result, attr) {
    result[attr] = elem.getAttribute('ows_' + attr);
    return result;
  }, {});
});
console.log(resultArry);
&#13;
<z:row ows_heading='Header' ows_startDate='1/1/2016' ows_finishDate='1/11/2016' ows_Description='Ignore me'/>
<z:row ows_startDate='2/3/2016' ows_finishDate='2/12/2016' ows_Description='Ignore me' ows_heading='Header' />
&#13;
&#13;
&#13;

与此代码的不同之处在于它使用属性名称而不是位置

答案 1 :(得分:0)

只需循环遍历元素,并为每个循环遍历其属性并提取属性值?

您的结果需要是一个对象数组:

[ 
   { "heading": "Header", "startDate": "1/1/2016", "finishDate": "1/11/2016" },
   { "heading": "Header", "startDate": "2/3/2016", "finishDate": "2/12/2016" }
]

因此:["heading": "Header", "startDate": "1/1/2016", "finishDate": "1/11/2016"]不是有效的结构。

&#13;
&#13;
var arry = ["heading", "startDate", "finishDate"];

var elems = document.getElementsByTagName("z:row");

var resultArry = [];

// Loop through the XML elements
for(var i = 0; i < elems.length; ++i){
  
  // For each element, create an object
  var obj = {};
  
  // Loop through the element's attributes
  for(var x = 0; x < elems[i].attributes.length - 1; ++x){
    // For each attribute, create a property value
    obj[arry[x]] = elems[i].attributes[x].value;
  }
  resultArry.push(obj);
}

console.log(resultArry);
&#13;
<z:row ows_heading='Header' ows_startDate='1/1/2016' ows_finishDate='1/11/2016' ows_Description='Ignore me'/>
<z:row ows_heading='Header' ows_startDate='2/3/2016' ows_finishDate='2/12/2016' ows_Description='Ignore me'/>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果我理解正确,你在变量中有xml字符串。在这种情况下,你可以这样做:

&#13;
&#13;
strlen(array);
&#13;
var uniqHeaderVals = ["heading", "startDate", "finishDate"]

var xml = "<z:row ows_heading='Header' ows_startDate='1/1/2016' ows_finishDate='1/11/2016' ows_Description='Ignore me'/>"
    + "<z:row ows_heading='Header' ows_startDate='2/3/2016' ows_finishDate='2/12/2016' ows_Description='Ignore me'/>";

var r = $('<content>' + xml + '</content>'); // valid XML needs a single root

var a = r.find('z\\:row').get().map(function (node) {
    return uniqHeaderVals.reduce( function (o, title) {
        var attr = $(node).attr('ows_' + title);
        if (attr) o[title] = attr;
        return o;
    }, {})
});

console.log(a);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 3 :(得分:0)

&#13;
&#13;
const XML_INPUT = `
<z:row ows_heading='Header' ows_startDate='1/1/2016' ows_finishDate='1/11/2016' ows_Description='Ignore me'/>
<z:row ows_heading='Header' ows_startDate='2/3/2016' ows_finishDate='2/12/2016' ows_Description='Ignore me'/>`;
const COLUMNS = ['heading', 'startDate', 'finishDate'];

console.log(children(toDom(XML_INPUT)).reduce(toRow, []));

function toDom(str) {
    var tmp = document.createElement('div');
    tmp.innerHTML = str;
    return tmp;
}

function children(nodes) {
  return [...nodes.querySelectorAll('*')];
}

function toRow(p, node) {    
    p.push(COLUMNS.reduce((p, c) => {
        p[c] = attr(node, c);
        return p;
    }, {}));
    return p;
}

function attr(node, name) {
    return node.getAttribute('ows_' + name);
}
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;