JS - 使用Arrays上的forEach循环将数据推送到JSON结构中

时间:2017-07-28 17:52:50

标签: javascript arrays json node.js foreach

如果它与我输入的数组中的元素(jsonWithListOfStatesAndCounters)匹配,我试图提取JSON值(来自名为inputedJurisdictionArray的结构)。我的输入数组包含sting值,包括单个或多个州名(即var inputedJurisdictionArray = ["Iowa", "California, Indiana, Delaware", "Florida"])。此数组中的奇异状态值在结尾处正常处理,但多个状态值是棘手的。我正在使用split()将它们转换为另一个数组,以便逐个处理它们。每当这个输入数组中的一个状态与jsonWithListOfStatesAndCounters中的“状态”值匹配时,我将其提取到另一个JSON结构中,并在每个块的末尾将其推送到我的初始变量myJurisdictionJSON中。我遇到的问题是,一旦这些forEach循环完成,我仍然在myJurisdictionJSON中保留原始值,而不是应该提取的val和计数器。 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;
                    }
                }   
            }       
        });

1 个答案:

答案 0 :(得分:0)

我不完全确定输出是你想要的,但它很接近。

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