2017-04-18 70 views
0

我有一個文本文件的陣列,含有2種不同類型的數據:「名稱」和「動作」,每一種類型的重複隨機序列在整個文本文件:分組隨機序列爲對象

NAME: joe 
ACTION: running 
NAME: paul 
ACTION: walking 
ACTION: swimming 
NAME: mary 
NAME: joe 
ACTION: sleeping 
NAME: paul 
NAME: mary 
NAME: ken 
ACTION: jumping 
ACTION: running 
ACTION: eating 

我試圖創建對象的數組,通過拼接每個類型的數據,使每個對象只能容納一對名稱和操作的,就像這樣:

{ 
    「name」: joe, 
    「action」 : running 
} 

{ 
    「name: paul, 
    「action」 : walking swimming 
} 

{ 
    「name: mary joe, 
    「action」 : sleeping 
} 

{ 
    「name: paul mary ken, 
    「action」 : jumping running eating 
} 

到目前爲止,我已經使用嘗試簡單的循環來逐行遍歷文本文件,檢查一行是否包含NAME或AC TION,但我無法弄清楚連接並將值推入數組的邏輯,而不會陷入困境。我正在學習JavaScript,並且可以真正使用一點幫助來解決這個問題,在此先感謝。

+0

遍歷文本文件,並假設你有一個總名稱行。如果它是真的,你可以在名爲name的變量中連接一個字符串。如果不是,則創建另一個存儲該操作的變量,創建一個新對象,插入對象名稱和操作變量,將該對象推入該數組中,並將名稱變量恢復爲空。希望它有幫助 – FFdeveloper

+0

編輯:在該算法中,找到一個操作行後,您必須假設下一個操作仍然是一個操作並將其存儲在一個變量中。如果它不是一個動作,則必須將一個新對象插入數組並重新初始化用於存儲名稱和動作的變量 – FFdeveloper

回答

0

這裏有一個類似的解決方案,即凱文的。最大的區別是它創建新對象並將它們添加到數組中,而不是將它添加到現有值。希望能幫助到你!

var lines = `NAME: joe 
ACTION: running 
NAME: paul 
ACTION: walking 
ACTION: swimming 
NAME: mary 
NAME: joe 
ACTION: sleeping 
NAME: paul 
NAME: mary 
NAME: ken 
ACTION: jumping 
ACTION: running 
ACTION: eating` 


var entries = []; 
var current = null; 
lines.split("\n").forEach(function(entry) { 

    if (entry.indexOf('NAME:') > -1) { 

     var name = entry.substring(6); 

     // if the current instance hasn't been set yet, 
     // or there is already a value present, start a new object. 
     if (!current || current.action) { 
     current = { name: name, action: null }; 
     entries.push(current); 
     return; 
     } 

     // append the new name the current name list 
     current.name = `${current.name} ${name}`; 
     return; 
    } 

    // append the new action to the current action list. 
    // if action is null, just set the action as its initial value. 
    var action = entry.substring(8); 
    current.action = !current.action ? action : `${current.action} ${action}`; 
}); 

console.log(entries); 
+0

這對我來說非常合適,謝謝! – JensB

+0

順便說一下,''{current.name} $ {name}'',他們做了什麼或意味着什麼?我試着用普通的引號輸入它,但是這不起作用... – JensB

+0

@JensB它被稱爲「模板文字」。 ES6中的許多重要新功能之一。這裏有更多關於它的細節https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals –

0

您需要先按行分割文本字符串,然後遍歷每行並解析鍵和值。這裏有一個工作示例

var text = `NAME: joe 
 
ACTION: running 
 
NAME: paul 
 
ACTION: walking 
 
ACTION: swimming 
 
NAME: mary 
 
NAME: joe 
 
ACTION: sleeping 
 
NAME: paul 
 
NAME: mary 
 
NAME: ken 
 
ACTION: jumping 
 
ACTION: running 
 
ACTION: eating` 
 

 
// split the text in to an array of lines 
 
var lines = text.split("\n") 
 

 
var data = [] // the final mapped data 
 
var person = null // the person object to add actions to 
 

 
// loop through each line 
 
lines.forEach(function(line){ 
 
    
 
    // split the line into a key (NAME or ACTION) and the value 
 
    var vals = line.split(': ') 
 
    var key = vals[0] 
 
    var val = vals[1] 
 

 
    // if this line is for a new name, make a new "person" 
 
    if(key == 'NAME'){ 
 
    
 
    // save the current person to the final data 
 
    if(person) data.push(person) 
 
    
 
    // then create a new one 
 
    person = {name: val, actions: ''} 
 
    
 
    // add the action to the current person (if there is one) 
 
    }else if(person){ 
 
    person.actions += val 
 
    } 
 

 
}) 
 

 
console.log(data)

+0

我剛剛注意到這與您所需的輸出不完全匹配,但也許您可以從此處計算出它? –

0

您可以使用.match()RegExp/NAME.+(?=\s\n)/g/ACTION.+(?=\s\n)/g,以配合"NAME""ACTION".map().replace()開始的行與RegExp/^[A-Z:\s]+/更換"NAME""ACTION"":",和空格字符。

獲取最大陣列.length使用Math.max()設置爲for循環的最大迭代次數。 .push()新陣列對象;如果元素不在.length的數組索引處,請將空字符串""設置爲屬性的值。

var text = `NAME: joe 
 
ACTION: running 
 
NAME: paul 
 
ACTION: walking 
 
ACTION: swimming 
 
NAME: mary 
 
NAME: joe 
 
ACTION: sleeping 
 
NAME: paul 
 
NAME: mary 
 
NAME: ken 
 
ACTION: jumping 
 
ACTION: running 
 
ACTION: eating`; 
 

 
var [res, ...[names, actions]] = [[] 
 
           , text.match(/NAME.+(?=\s\n)/g) 
 
           , text.match(/ACTION.+(?=\s\n)/g) 
 
           ].map(function(arr) { 
 
            return arr.map(function(value) { 
 
             return value.replace(/^[A-Z:\s]+/, "") 
 
            }) 
 
            }); 
 

 
for (var i = 0; i < Math.max(names.length, actions.length); i++) { 
 
    res.push({name:names[i] || "", action: actions[i] || ""}) 
 
}; 
 

 
console.log(res);