web-dev-qa-db-ja.com

配列値をオブジェクトキーに変換する

私はjsonオブジェクトを返すgetを行います:

        "data": [
[
"2016 Pass/Fail Rates by Test Centre",
"",
"",
"",
"",
"",
"",
"",
"",
""
],
[
"",
"Passes",
"",
"No ID",
"",
"Fails",
"",
"Fail Dangerous",
"",
"Total"
],
[
"Sometown",
"8,725",
"53.40%",
"140",
"0.90%",
"7,417",
"45.40%",
"48",
"0.30%",
"16,330"
],
[
"Some Other Town",
"12,778",
"44.80%",
"193",
"0.70%",
"15,422",
"54.10%",
"103",
"0.40%",
"28,496"
],
[... many more identically formatted arrays ...]

そして、私は次のようになりたいです:

[{"Location":"Sometown", "Passes":8,725, "Pass%":53.40%, "No ID":140, "NoID%":0.90%, "Fails":7,417, "Fail%":45.40%, "Fail Dangerous":48, "FailDangerous%":0.30%, "Total":16,330}, {"Location":"Some Other Town", "Passes":8,725, etc etc...

したがって、「データ」配列内の最初の配列を無視し、2番目の配列の値をキーとして使用し(空の文字列をより有用なものに置き換えます)、残りの配列の値を結果オブジェクトの値として使用します。

したがって、かなり単純な問題ではないかと思われますが、これはかなり複雑な問題です。それを行う最も簡単な方法は何ですか?また、異なる場合は、処理/ページの負荷の観点から最も簡単な方法は何ですか?

前もって感謝します、

7
protoskull

2番目の配列を必要なオブジェクトのキーとして使用し、キーの後の部分のみを反復処理できます。次に、キーを反復処理し、配列の値用の新しいオブジェクトを作成します。新しい配列のマッピング用のオブジェクトを返します。

var data = { data: [["2016 Pass/Fail Rates by Test Centre", "", "", "", "", "", "", "", "", ""], ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"], ["Sometown", "8,725", "53.40%", "140", "0.90%", "7,417", "45.40%", "48", "0.30%", "16,330"], ["Some Other Town", "12,778", "44.80%", "193", "0.70%", "15,422", "54.10%", "103", "0.40%", "28,496"]] },
    keys = data.data[1],
    result = data.data.slice(2).map(function (a) {
        var temp = {};
        keys.forEach(function (k, i) {
            temp[k] = a[i];
        })
        return temp;
    });
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
Nina Scholz

オブジェクトにインデックスと値を追加しながら、配列を反復処理できます...

オブジェクトと配列を反復処理する方法の例 here および例 here を見つけることができます。

通常、アイテムをオブジェクトに追加するには

var array = [{"Location":"Sometown"}, {"Location2":"Sometown2"},    {"Location3":"Sometown3"}],
object = {};
array.forEach(function(element, index) {
    object[index] = element;
});
console.log(object);
4
McKabue

ここに私の解決策があります。それがあなたとこの問題と混同している人に役立つことを願っています!

  1. 意味のあるオブジェクトを構築する必要があります!
const meaningful_objs = {
    "Location": "",
    "Passes": "",
    "Passes%": "",
    "No ID": "",
    "No ID%": "", 
    "Fails": "",
    "Fails%": "",
    "Fail Dangerous": "",
    "Fail Dangerous%": "",
    "Total": ""
};

// or get it from you data[1], 
// but I think it has some errors of you got returned data!
// It should be have all keys!

// just make a assume, you get the right data[1]!

const data[1] = ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]; 

let keys = data[1];
/*

Array to Object

*/




// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]

const keys_array = [
    "Location",
    "Passes",
    "Passes%",
    "No ID",
    "No ID%", 
    "Fails",
    "Fails%",
    "Fail Dangerous",
    "Fail Dangerous%",
    "Total"
];

let temp_obj = {};

keys_array.map(
    (value, index) => {
        temp_obj[`${value}`] = "";
        // temp_obj[value] = "";
        return temp_obj;
    }
);

console.log(`temp_obj = `, temp_obj);
// {"Location": "","Passes": "","Passes%": "","No ID": "","No ID%": "", "Fails": "","Fails%": "","Fail Dangerous": "","Fail Dangerous%": "","Total": ""};



for(let k in temp_obj) {
    console.log(`typeof (k) = `, typeof (k));
    // typeof (k) = string
}




/*

Object to Array

*/


const meaningful_objs = {
    "Location": "",
    "Passes": "",
    "Passes%": "",
    "No ID": "",
    "No ID%": "", 
    "Fails": "",
    "Fails%": "",
    "Fail Dangerous": "",
    "Fail Dangerous%": "",
    "Total": ""
};

// {"Location": "","Passes": "","Passes%": "","No ID": "","No ID%": "", "Fails": "","Fails%": "","Fail Dangerous": "","Fail Dangerous%": "","Total": ""};




let keys_array = Object.keys(meaningful_objs);

console.log(`keys_array = \n`, keys_array);

// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]






/*

result 

*/



let keys = keys_array;
// ["Location", "Passes", "Passes%", "No ID", "No ID%", "Fails", "Fails%", "Fail Dangerous", "Fail Dangerous%", "Total"]


let arrays = [
    [
        "Sometown",
        "8,25",
        "53.40%",
        "140",
        "0.90%",
        "7,17",
        "45.40%",
        "48",
        "0.30%",
        "16,30"
    ],
    [
        "Some Other Town",
        "12,78",
        "44.80%",
        "193",
        "0.70%",
        "15,22",
        "54.10%",
        "103",
        "0.40%",
        "28,96"
    ]
];

let result = arrays.map(
    (array) => {
        let temp = {};
        keys.forEach(
            (key, index) => {
                console.log(`key = `, key);
                console.log(`index = `, index);
                temp[`${key}`] = array[index];
            }
        );
        console.log(`temp = `, temp);
        return temp;
    }
);
console.log(`result = `, result);



/*

test

*/


let string_objs = JSON.stringify(result);

// "[{"Location":"Sometown","Passes":"8,25","Passes%":"53.40%","No ID":"140","No ID%":"0.90%","Fails":"7,17","Fails%":"45.40%","Fail Dangerous":"48","Fail Dangerous%":"0.30%","Total":"16,30"},{"Location":"Some Other Town","Passes":"12,78","Passes%":"44.80%","No ID":"193","No ID%":"0.70%","Fails":"15,22","Fails%":"54.10%","Fail Dangerous":"103","Fail Dangerous%":"0.40%","Total":"28,96"}]"


let string_obj1 = JSON.stringify(result[0]);

// "{"Location":"Sometown","Passes":"8,25","Passes%":"53.40%","No ID":"140","No ID%":"0.90%","Fails":"7,17","Fails%":"45.40%","Fail Dangerous":"48","Fail Dangerous%":"0.30%","Total":"16,30"}"


/*


    {
        "Location":"Sometown",
        "Passes":"8,25",
        "Passes%":"53.40%",
        "No ID":"140",
        "No ID%":"0.90%",
        "Fails":"7,17",
        "Fails%":"45.40%",
        "Fail Dangerous":"48",
        "Fail Dangerous%":"0.30%",
        "Total":"16,30"
    }

*/


let string_obj2 = JSON.stringify(result[1]);


// "{"Location":"Some Other Town","Passes":"12,78","Passes%":"44.80%","No ID":"193","No ID%":"0.70%","Fails":"15,22","Fails%":"54.10%","Fail Dangerous":"103","Fail Dangerous%":"0.40%","Total":"28,96"}"


/*

    {
        "Location":"Some Other Town",
        "Passes":"12,78",
        "Passes%":"44.80%",
        "No ID":"193",
        "No ID%":"0.70%",
        "Fails":"15,22",
        "Fails%":"54.10%",
        "Fail Dangerous":"103",
        "Fail Dangerous%":"0.40%",
        "Total":"28,96"
    }


*/
2
user8202629