0

In a part of my code, I am extracting data from a JSON file and place it in an object based array.

I receive the data successfully and can put each on on single object within an array. the only issue is, I want to give a name to each object.

var playlist_data = {
  "Music1": {
    "soundcloud": "Soundcloud Music 1",
    "spotify": "Spotify Music 1"
  },
  "Music2": {
    "soundcloud": "Soundcloud Music 2",
    "spotify": "Spotify Music 2"
  },
  "Music3": {
    "soundcloud": "Soundcloud Music 3",
    "spotify": "Spotify Music 3"
  },
  "Music4": {
    "soundcloud": "Soundcloud Music 4",
    "spotify": "Spotify Music 4"
  }
};
var links = [];
$.each(playlist_data, function(index, element) {
  links.push({
    spotify: element.spotify,
    soundcloud: element.soundcloud,
  });
});

console.log(links);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

In the above code seems like replicating the same JSON data but it is a simplified version.

So in result I would like to call it like links.Music2 and links.Music2.spotify

Heretic Monkey
  • 10,498
  • 6
  • 45
  • 102
Danny
  • 79
  • 3
  • 2
    You cannot name elements in an array, so you need to use an object. *However* then you are back to the *exact* same data structure your JSON has, so your loop is completely redundant – Rory McCrossan Jun 02 '19 at 19:46
  • @RoryMcCrossan Yes thats right. But the main json file is way longer than this with various data. So I like to make a smaller version of main json with specific data. – Danny Jun 02 '19 at 19:51
  • 1
    Possible duplicate of [How to get a subset of a javascript object's properties](https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties) See also [One-liner to take some properties from object in ES 6](https://stackoverflow.com/q/25553910/215552) – Heretic Monkey Jun 02 '19 at 19:54
  • 1
    and [Javascript give array Key a name while doing an array 'push'](https://stackoverflow.com/q/21406185/215552) – Heretic Monkey Jun 02 '19 at 19:59

3 Answers3

0

Since Javascript does not support named indexes, you would have to make links an object in order to access its properties via links.Music2.spotify. This is easily done however, it would just give you the exact same data you began with:

var playlist_data = {
  "Music1": {
    "soundcloud": "Soundcloud Music 1",
    "spotify": "Spotify Music 1"
  },
  "Music2": {
    "soundcloud": "Soundcloud Music 2",
    "spotify": "Spotify Music 2"
  },
  "Music3": {
    "soundcloud": "Soundcloud Music 3",
    "spotify": "Spotify Music 3"
  },
  "Music4": {
    "soundcloud": "Soundcloud Music 4",
    "spotify": "Spotify Music 4"
  }
};
var links = {};
$.each(playlist_data, function(index, element) {
  links[index]= {
    spotify: element.spotify,
    soundcloud: element.soundcloud,
  };
});

console.log(links);
console.log(links.Music2.spotify)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Fdebijl
  • 656
  • 3
  • 15
-1

you can name your each obj with code like below

var playlist_data = {
  "Music1": {
    "soundcloud": "Soundcloud Music 1",
    "spotify": "Spotify Music 1"
  },
  "Music2": {
    "soundcloud": "Soundcloud Music 2",
    "spotify": "Spotify Music 2"
  },
  "Music3": {
    "soundcloud": "Soundcloud Music 3",
    "spotify": "Spotify Music 3"
  },
  "Music4": {
    "soundcloud": "Soundcloud Music 4",
    "spotify": "Spotify Music 4"
  }
};
var links = [];
$.each(playlist_data, function(index, element) {
  links.push({
    spotify: element.spotify,
    soundcloud: element.soundcloud,
  });
});

let myMusic = {};

for(let x = 0 ; x < links.length ; x++)
{
    let z = Number(x+1);
    myMusic["music"+z] = links[x];
}

console.log(myMusic.music1);
-1

As it seems you want to use the spotify as a key for the links.

There are two types of array in JS:

standard array which is : [ ]

associative array which is : { }!

As you see you can use { } which also stands as an object in javascript as an array. And then you can use spotify as the key. So your code will be like below:

var links = {};
$.each(playlist_data, function(index, element) {
  links[index] = {
    spotify: element.spotify,
    soundcloud: element.soundcloud,
  };
});
console.log(links_s.Music1.spotify) // Spotify Music 1
Alireza HI
  • 1,673
  • 1
  • 9
  • 20