how to make dynamic javascript array with key and value group [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am integrating jqbarGraph, the demo worked fine. I want to make the values dynamic.
this is my json response
{"success":false,"message":{"12":7887,"11":159}}
I need an array like this
graphByMonth = new Array(
[7887,12],
[159,11]
);
Need to create dynamic array from JSON data.

Does this work?
var graphByMonth = [],
jsonResponse = {"success":false,"message":{"12":7887,"11":159}},
data = jsonResponse.message;
Object.keys(data).forEach(function (k, i ) {
graphByMonth.push([data[k],+k]);
});
console.log(graphByMonth);

Related

How to iterate and append characters of an array by push [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
var charArray=[a,b,c]
I want to have a new array as following:
var charArrayNew=[a, ab, abc]
Please suggest how to get the result of charArrayNew in ES6 Javascript.
You could do this:
const charArray=['a','b','c'];
const answer = charArray.map((_,i,ar)=>ar.slice(0,i+1).join(""));
console.log(answer);
// or, alternatively:
fn=(ar,res=[])=>(
ar.reduce((a,c)=>(res.push(a+=c),a),""),res );
console.log(fn(charArray))

How to randomly render products after get API to obtain data in Vue? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
this.$http.get(api).then(response => {
vm.products = response.data.products;
});
If i have data about products and i want to randomly render products what should i do?
You can set a computed property where you shuffle your products list, something like this:
computed {
shufflesProducts() {
return vm.products.sort(() => Math.random() - 0.5);
}
}

REMOVE SPECIFIC NUMBER WITH JS [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'M TRYING TO HANDLE THIS DATA IN JAVASCRIPT SO HOW CAN I REMOVE DATA ACCORDING TO THE ID NUMBER THE FIRST NUMBER
600030/01/2018/163904
600010/01/2014/3789
600030/01/2020/47104
600030/01/2012/39104
600010/01/2011/93817
how can i remove all 600030 data so i get
600010/01/2014/3789
600010/01/2011/93817
If all data shaped in Array and element is instance of String.
I would recommend Array.prototype.filter and String.prototype.startsWith();
let data = [
'600030/01/2018/163904',
'600010/01/2014/3789',
'600030/01/2020/47104',
'600030/01/2012/39104',
'600010/01/2011/93817',
];
let filteredData = data.filter((d)=>!d.startsWith('600030'));
console.log( filteredData );
Result is
[
'600010/01/2014/3789',
'600010/01/2011/93817'
]

How can I assign variable from json? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So my codes are like below;
$.getJSON('https://api.myjson.com/bins/gxs81',function(data){
$.each(data.employees,function(i,emp){
$('ul').append('<li>'+emp.firstName+' '+emp.lastName+'</li>');
});
I have multiple objects in json file, how can I assign them in an array? e.g.
array[2][0]=*firstName field should be here*
array[2][1]=*lastname field should be here*
Here's a simple push() to build a new array from your JSON:
$.getJSON('https://api.myjson.com/bins/gxs81',function(data){
var current = '';
var myarray = [];
$.each(data.employees,function(i,emp){
current = emp.firstName+' '+emp.lastName;
$('ul').append('<li>'+current+'</li>');
myarray.push(current);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul></ul>

Javascript Array to special Object [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I need for an Plugin an Object structure like this:
{
"Beer":145.53,
"Apple (red)":7.33,
"Apple (green)":0.03
}
Don't know how to write it that this shine.
I got the all values already so how to set it up like this?
Use this notation to make objects with those properties:
var obj = {};
obj["Beer"] = 145;
obj["Apple (red)"] = 7.5;
....

Categories

Resources