[英]JS - Pushing data into JSON structure using forEach loops on Arrays
我嘗試從與輸入數組( inputedJurisdictionArray
)中的元素匹配的JSON值(從名為jsonWithListOfStatesAndCounters
結構中提取)。 我輸入的數組包含包含單個或多個狀態名稱的字符串值(即var inputedJurisdictionArray = ["Iowa", "California, Indiana, Delaware", "Florida"]
)。 通常在結尾處正常處理此數組中的奇異State值,但在多個State值中比較棘手。 我正在使用split()
以便將它們轉換為另一個數組,以便可以對其進行逐個處理。 jsonWithListOfStatesAndCounters
此輸入數組中的狀態之一與jsonWithListOfStatesAndCounters
的“狀態”值jsonWithListOfStatesAndCounters
,我jsonWithListOfStatesAndCounters
其提取到另一個JSON結構中,並將其在每個塊的末尾推入我的初始變量myJurisdictionJSON
。 我遇到的問題是,一旦完成這些forEach循環,我仍然將自己的原始值留在myJurisdictionJSON
,而不是應該提取的val和counter。 jsonWithListOfStatesAndCounters
肯定包含應該與我的inputedJurisdictionArray
的元素匹配的值,但是該信息沒有被推送到myJurisdictionJSON
。 我究竟做錯了什么? 任何提示/指針都會有所幫助。
var myJurisdictionJSON = [{
jurisdiction_val: 'jurisdiction_val',
jurisdiction_counter: 'jurisdiction_counter'
}];
inputedJurisdictionArray.forEach(function each(item) {
if (Array.isArray(item)) {
item.forEach(each);
} else {
var jurisdictionInput = item;
jsonWithListOfStatesAndCounters.forEach(function each(item) {
if (Array.isArray(item)) {
item.forEach(each);
} else {
if (jurisdictionInput.includes(",") === true){//Checking if more than one jurisdiction in string
var jurisdictionArr = jurisdictionInput.split(", ");
var jurisdictionCounter = item.jurisdictionCounter;
var jurisdictionState = item.jurisdictionState;
jurisdictionArr.forEach(function(element) {
if (myJurisdictionJSON.jurisdiction_counter == 'jurisdiction_counter'){ // If nothing is pushed into our predefined JSON object
if (jurisdictionState.toLowerCase() == trim(element.toLowerCase())) {
var jurisdictionJSON_inner = {
jurisdiction_val: element,
jurisdiction_counter: jurisdictionCounter
};
myJurisdictionJSON.push(jurisdictionJSON_inner);
return;
}
}else if (myJurisdictionJSON.jurisdiction_counter != 'jurisdiction_counter'){ // if an item has been pushed into myJurisdictionJSON, append the next items
var jurisdictionCounter = item.jurisdictionCounter;
var jurisdictionState = item.jurisdictionState;
if (jurisdictionState.toLowerCase() == trim(jurisdictionInput.toLowerCase())) {
jurisdictionJSON_inner.jurisdiction_val = jurisdictionJSON_inner.jurisdiction_val + ", " + jurisdictionInput;
jurisdictionJSON_inner.jurisdiction_counter = jurisdictionJSON_inner.jurisdiction_counter + ", " + jurisdictionCounter;
myJurisdictionJSON.push(jurisdictionJSON_inner);
return;
}
}
});
}
else{// if only one jurisdiction state in jurisdictionInput string
var jurisdictionCounter = item.jurisdictionCounter;
var jurisdictionState = item.jurisdictionState;
if (jurisdictionState.toLowerCase() == trim(jurisdictionInput.toLowerCase())) {
var jurisdictionJSON_inner = {
jurisdiction_val: jurisdictionInput,
jurisdiction_counter: jurisdictionCounter
};
myJurisdictionJSON.push(jurisdictionJSON_inner);
return;
}
}
}
});
我不能完全確定輸出是否是您想要的,但是已經很接近了。
// input data as per your example let inputedJurisdictionArray = [ 'Iowa', 'California, Indiana, Delaware', 'Florida' ]; // I had to make this part up. It's missing from the example let jsonWithListOfStatesAndCounters = [{ jurisdictionCounter: 2, jurisdictionState: 'Florida' }, { jurisdictionCounter: 4, jurisdictionState: 'Indiana' }, { jurisdictionCounter: 3, jurisdictionState: 'Texas' } ]; // first, fix up inputedJurisdictionArray // reduce() loops over each array element // in this case we're actually returning a LARGER // array instead of a reduced on but this method works // There's a few things going on here. We split, the current element // on the ','. Taht gives us an array. We call map() on it. // this also loops over each value of the array and returns an // array of the same length. So on each loop, trim() the whitespace // Then make the accumulator concatenate the current array. // Fat arrow ( => ) functions return the results when it's one statement. inputedJurisdictionArray = inputedJurisdictionArray.reduce( (acc, curr) => acc.concat(curr.split(',').map(el => el.trim())), [] ); // now we can filter() jsonWithListOfStatesAndCounters. Loop through // each element. If its jurisdictionState property happens to be in // the inputedJurisdictionArray array, then add it to the // myJurisdictionJSON array. let myJurisdictionJSON = jsonWithListOfStatesAndCounters.filter(el => inputedJurisdictionArray['includes'](el.jurisdictionState) ); console.log(myJurisdictionJSON);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.