简体   繁体   中英

how to store JSON data into key-value pairs in javascript?

how to store JSON data into key-value pairs in javascript?

I have a json data file : data.js It contains follwing data

[
    { "Sound": "0.550951615" },
    { "Battery": 0.2567320563 },
    { "Screen": 0.0703125 },
    { "RAM": 0.1246117102 },
    { "Connectivity": 0 },
    { "Camera": 0.2721623766 },
    { "Design": 0.3677622768 },
    { "Processor": 0 }
]

how to store this data in key-value pair data-structure in java script?

You can use Array.map or Array.reduce (see MDN ) to map the Array elements to an object, something like:

 var objmap = {}, objred = {}; // parse the string (from your file) var objFromFile = JSON.parse('[\\ { "Sound": "0.550951615" },\\ { "Battery": 0.2567320563 },\\ { "Screen": 0.0703125 },\\ { "RAM": 0.1246117102 },\\ { "Connectivity": 0 },\\ { "Camera": 0.2721623766 },\\ { "Design": 0.3677622768 },\\ { "Processor": 0 }\\ ]'); // and map it to [obj] var mapped = objFromFile.map( function (v) { for (var l in v) {this[l] = v[l];} }, objmap ); // or use reduce if you prefer it var reduced = objFromFile.reduce( function(p, n) { for (var l in n) {p[l] = n[l];} return p; }, objred ); // show the result, using JSON.stringify document.querySelector('#result').textContent = 'mapped:\\n' + JSON.stringify(objmap, null, ' ') + '\\n\\nreduced:\\n' + JSON.stringify(objred, null, ' '); 
 <pre id="result"></pre> 

JSON stands for JavaScript Object Notation. This is a systematic Syntax to store data.

for storing single value:

var Object1 = {
  "Key": "Value"
}

for storing multiple value:

var Object1 = {
  "Key1": "Value1",
  "Key2": "Value2",
  "Key3": "Value3",
  "nth key": "nth value"
}

You need to process your data structure to get this done.

var a = [
    { "Sound": "0.550951615" },
    { "Battery": 0.2567320563 },
    { "Screen": 0.0703125 },
    { "RAM": 0.1246117102 },
    { "Connectivity": 0 },
    { "Camera": 0.2721623766 },
    { "Design": 0.3677622768 },
    { "Processor": 0 }
]
var keyValue = {}
a.forEach(function(e) {
    keyValue[Object.keys(e)[0]] = Object.values(e)[0]
})

Output of console.log(keyValue)

{"Sound":"0.550951615","Battery":0.2567320563,"Screen":0.0703125,"RAM":0.1246117102,"Connectivity":0,"Camera":0.2721623766,"Design":0.3677622768,"Processor":0}
var myObject = { 
 key: value,
 key: value
 };

You would literally just list your contents in that format.

Other Resources: http://www.w3schools.com/js/js_objects.asp

How to "properly" create a custom object in JavaScript?

You're already storing the data as key:value.

If you're wondering how you can access the various bits and pieces of your data, checkout this article: JavaScript loop through json array?

OR ...

You can find a particular k:v pair by index. In your case, if you store the payload in a variable, you could access the data like so:

var data = [
    { "Sound": "0.550951615" },
    { "Battery": 0.2567320563 },
    { "Screen": 0.0703125 },
    { "RAM": 0.1246117102 },
    { "Connectivity": 0 },
    { "Camera": 0.2721623766 },
    { "Design": 0.3677622768 },
    { "Processor": 0 }
]

data[0]

This would return { "Sound": "0.550951615" }

There are a TON of resources that you can search for JSON references.

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM