2016-11-30 3 views
0

からの値を持つオブジェクトを作成します。私は、一意の列名のリストを含むオブジェクトを持っている:は、アレイ

<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'/> 

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

私はこの本に似たようなXMLデータセットを返すいます

ユニークな列名オブジェクトをループして列名を取得し、その列に「ows_」を追加して結果の値を見つけてオブジェクトに追加して、最終結果が次のようになるようにします。

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

EDIT:現在のコードブロック:その属性によって要素を通って、それぞれ1つのループの

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); 
+0

あなたが何をしようとしたことがありますか?配列をループする方法を尋ねていますか? –

答えて

1

@ScottMarcusのビルはほとんど正解

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);
<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' />

このコードの違いではなく位置

よりも、それは属性名を使用していることです
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"]は有効な構造ではありません。

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);
<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'/>

+0

'z:row'の属性の順序が' arry'の順序と異なる場合、コードに欠陥があります –

+0

本当ですが、OPがXMLスキーマを教えてくれたので、私のコードには欠陥がありません。 –

+0

スキーマは属性の順序を指示しません –

0

私が正しく理解すれば、変数にxml文字列があります。その場合、あなたはこのようにそれを行うことができます:

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);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

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); 
 
}
.as-console-wrapper { max-height: 100% !important; top: 0; }