How can I convert the following data structure:
var data = [ [ { time: 1, speed : 20 } ] ];
to
var data = [ { time: 1, speed: 54 } ];
I just want to remove the array.
As the data is an array, you just want to select the first element of the outer array
so the solution would be
var data = [[{time:1,speed:20}]]; // Or whatever the data is
data = data[0];
Or if you're accessing the data via another object
var data = yourObject[0];
Try this :
JSON.stringify(data).substr(1,JSON.stringify(data).length-2);
Related
I need to have in array with lat/lon points like that:
/*
var polylinePoints = [
[37.781814, -122.404740],
[37.781719, -122.404637],
[37.781489, -122.404949],
[37.780704, -122.403945],
[37.780012, -122.404827]
];
*/
But I need first to sort it by third parameter which is timestamp?
How to do that? I know how to do that in PHP but not in JS
var polylineTimestamp = [
[37.781814, -122.404740, 1666543938],
[37.781719, -122.404637, 1666543938],
[37.781489, -122.404949, 1666543938],
[37.780704, -122.403945, 1666543938],
[37.780012, -122.404827, 1666543938]
];
Then I need to delete (trim) sorted array (delete timestamp) to have something like polylinePoints.
Here is Jsfiddle:
https://jsfiddle.net/qsdaLz7h/
Array .sort() and .map() will get you there. You could combine them, but it'll be easier for you to follow the logic when they're separated, as below.
// I changed your original timestamps to give some difference
var polylineTimestamp = [
[37.781814, -122.404740, 1666543958],
[37.781719, -122.404637, 1666543948],
[37.781489, -122.404949, 1666543968],
[37.780704, -122.403945, 1666543938],
[37.780012, -122.404827, 1666543998]
];
// sort polylineTimestamp by third parameter (timestamp) older first
var sortedarray = polylineTimestamp.sort((a,b)=> {
return a[2] - b[2];
});
// Remove timestamp from resulting array
var polylinePoints = sortedarray.map(el => {
return [el[0],el[1]];
});
// Log to console
console.log(polylinePoints)
u can make a temp var
let arr = []
polylineTimestamp.forEach((el) => {
arr.push([el[0],el[1]])
})
console.log(arr)
//! expected output would be
arr = [
[ 37.781814, -122.40474 ],
[ 37.781719, -122.404637 ],
[ 37.781489, -122.404949 ],
[ 37.780704, -122.403945 ],
[ 37.780012, -122.404827 ]
]
you also can get a new arr the it can filter to not get the index 2 from the origin arr
Here is your answerer:
const TIMESTAMP_POSITION = 2
var polylineTimestamp = [
[37.781814, -122.404740, 1666540000],
[37.781719, -122.404637, 1666541000],
[37.781489, -122.404949, 1666542000],
[37.780704, -122.403945, 1666543000],
[37.780012, -122.404827, 1666544000]
];
polylineTimestamp.sort(function (a, b) {
// Turn your timestamp number into dates, and then subtract them
// to get a value that is either negative, positive, or zero.
return new Date(b[TIMESTAMP_POSITION]) - new Date(a[TIMESTAMP_POSITION]);
})
//map to remove timestamp
var polyLineArray = polylineTimestamp.map(function (polyLine) {
return [polyLine[0], polyLine[1]]
})
I used the sort function to sort your initial array using date conversion from timestamp.
when just mapping the array to remove the timestamp
Good day! I badly need help for this one, I have an array with many elements/data that is needed to be display in textbox. Each array element/data must be inside the textbox. (The Textbox must be dynamically set up using loop with the array data inside it)
arr = ["1"-"2"-"3"-"4"-"5"]; //my array is from the db, this is example only
conv_arr = arr.split("-")
var myArray = [conv_arr];
var ArrayInText = document.createElement('input');
myArray.forEach(function(conv_arr) {
ArrayInText.value = conv_arr ;
document.body.appendChild(ArrayInText);
It displays the array (pretend this is a textbox [ ])
[ 1, 2, 3, 4, 5 ]
I want a result that looks like this (One textbox per element using loop)
[ 1 ] [ 2 ] [ 3 ] [ 4 ] [ 5 ]
You can see the demo here=> https://jsfiddle.net/4ow6k8j5/1/
After removing unnecessary assignments, you can use below simplest solution;
conv_arr = arr.split("-")
conv_arr.forEach(function(elem) {
var ArrayInText = document.createElement('input');
ArrayInText.value = elem ;
document.body.appendChild(ArrayInText);
});
I have data coming in from the db I need to store in a js array of objects. How do I initialize this js array of objects , so that I can keep adding the data when I get it from the db .
The object structure will be of this format
var OsNames = [{deviceName:"TV",
releases: [
{release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"}]},
{release: "rel2", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]}
]},
{deviceName:"Phone",
releases: [
{release: "rel1", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]},
{release: "rel2", hws: [{hw:"h1"},{hw:"h2"},{hw:"h3"}]}]
}];
var OsNames = [];
You can add data to Array by pushing elements in it when you get it from the db
e.g.
OsNames.push({release: "rel1", hws : [{hw:"h1"},{hw:"h2"},{hw:"h3"});
I think that is what you are looking for.
var osNames = [];
// Get Data from Database
var myData = ajax.get(...);
for (var i=0; i < myData.length; i++) {
osNames.push(myData[i]);
}
Im trying to create a structure with Javascript as follows:
var users = {
user.id: {
session.id1: session.id1,
session.id2: session.id2,
session.id3: session.id3
},
user.id2: {
session.id1: session.id1,
session.id2: session.id2,
session.id3: session.id3
},
};
What i need: add new sessions and remove them, removing okay, but how should i define object and how can i push new sessions to user obejct? That's why key is equal to value.
If you want to use session.id1 instead of something like sessionId1 :
Assign value:
users['user.id'].['session.id1'] = value;
Create object:
var users = {
'user.id': {
'session.id1': session.id1,
'session.id2': session.id2,
'session.id3': session.id3
},
'user.id2': {
'session.id1': session.id1,
'session.id2': session.id2,
'session.id3': session.id3
},
};
But I don't recommend it. If you are the only one who is gonna work with this code, it's ok.
You can first create an empty object and fill it as and when the data comes like
users[user.id] = {};
For an example:
var users = {};
var user = {id : 1}; //Data received(Just an example)
users[user.id] = {};
var session = {id1 : 1.1}; //Data received
users[user.id][session.id1] = session.id1;
console.log(JSON.stringify(users));
How about refactoring the user object to store sessions as an array and push, pop and slice them as required.
var users = [
{
id:'userid',
sessions: [
{
id: 'sessionid',
sessiondata: something
},
{
id: 'sessionid',
sessiondata: something
}
]
}];
This way to can just use normal array operators on the session array for each user.
[
[
{"path":"path2","value":"kkk"},
{"path":"path0","value":"uuu"},
{"path":"path1","value":"ppp"}
]
]
I get above result from for my manipulation, But I need it as follows.
["path":"path2","value":"kkk"],
["path":"path0","value":"uuu"],
["path":"path1","value":"ppp"]
Here is my code:
$scope.sharePaths[d.id] = []
d.conf.paths = []
$scope.sharePaths[d.id][index] = []
commaPath = 'kkk,uuu,ppp'
a = commaPath.split(',')
for key of a
value = a[key]
$scope.sharePaths[d.id][index].push {'path':'path'+key, 'value':a[key]}
d.conf.paths.push {'path':'path'+key, 'value':a[key]}
Just use the first element of your array. The variable data is already formatted correctly. The new format you want is not valid JSON.
var data = [[{"path":"path2","value":"kkk"},{"path":"path0","value":"uuu"},{"path":"path1","value":"ppp"}]];
data = data[0];