I'm trying to map the JSON from the URL to an array but I think my mapping of the data isn't correct. I want to find every value inside of attributes and count how many instances of each value there are in the JSON file/ array.
[
{
name: "1",
attributes: [
{
trait_type: "hat",
value: "blue"
},
{
trait_type: "hair",
value: "red"
}
]
}
];
$.getJSON(
"https://jsonware.com/api/v1/json/3c53cbcd-5351-4fba-8b89-5f1fb009e857",
function (data) {
var items = $.map(data.attributes, function (i) {
return i.value;
const result = data.reduce(
(acc, curr) => ((acc[curr] = (acc[curr] || 0) + 1), acc),
{}
);
console.log(result);
console.log(items);
});
}
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
It is not clear what you want from the call.
There will me no code run after return i.value; so the console.logs will not run
The values you are getting cannot be added.
Here I get an array of all values. jQuery not needed
fetch(
"https://jsonware.com/api/v1/json/3c53cbcd-5351-4fba-8b89-5f1fb009e857")
.then(response => response.json())
.then(data => {
const values = Object.values(data).flatMap(item => ({[item.name]:item.attributes.map(attr => attr.value )}))
values.forEach(val => console.log(Object.keys(val)[0],Object.values(val)[0].length))
const allValues = Object.values(data).map(item => item.attributes.map(attr => attr.value ))
console.log("All values: ",allValues.length,allValues)
}
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>