Taking 3 array elements and making them one formatted string
I am running an ajax request that returns JSON data:
{
"error":0,
"fleet":[
{
"fleet_uid":859805,
"purpose":0,
"ower":1,
"time":520,
"con_time":647,
"from":[
6,
300,
2
],
"target":[
6,
300,
6
],
"start_user_id":20457507089,
"target_user_id":20510481089,
"start_planet_name":"Tweenis12",
"target_planet_name":"P23808"
},
{
"fleet_uid":859803,
"purpose":0,
"ower":1,
"time":508,
"con_time":647,
"from":[
6,
300,
2
],
"target":[
6,
300,
6
],
"start_user_id":20457507089,
"target_user_id":20510481089,
"start_planet_name":"Tweenis12",
"target_planet_name":"P23808"
}
],
"count":2
}
I only need to get target information inside a float property. Depending on the time, there may be no fleet records, and there may be 10+.
There are 3 records inside the target property. I need to concatenate these 3 records into one string formatted as xx_xxx_xx
Being so unfamiliar with JS and jQuery, I'm not sure how to do this.
Try something like this,
var json = {"error":0,"fleet":[{"fleet_uid":859805,"purpose":0,"ower":1,"time":520,"con_time":647,"from":[6,300
,2],"target":[6,300,6],"start_user_id":20457507089,"target_user_id":20510481089,"start_planet_name":"Tweenis12"
,"target_planet_name":"P23808"},{"fleet_uid":859803,"purpose":0,"ower":1,"time":508,"con_time":647,"from"
:[6,300,2],"target":[6,300,6],"start_user_id":20457507089,"target_user_id":20510481089,"start_planet_name"
:"Tweenis12","target_planet_name":"P23808"}],"count":2};
var arr = [];
$.each(json.fleet, function(){
var value = this.target;
arr.push(value[0] + '_' + value[1] + '_' + value[2]);
});
alert(arr);
demo in FIDDLE
source to share
We can use a function map()
to compile all of your values target
for each element of the float into one array. This will help a lot when retrieving data. The values in each are target
concatenated with _
. This method does not require jQuery.
Run the snippet below. An important piece of code is the var targets
conditional declaration to check that your data has a property fleet
.
For the data you have in your question var targets
becomes ['6_300_6', '6_300_6']
.
var data = {"error":0,"fleet":[{"fleet_uid":859805,"purpose":0,"ower":1,"time":520,"con_time":647,"from":[6,300
,2],"target":[6,300,6],"start_user_id":20457507089,"target_user_id":20510481089,"start_planet_name":"Tweenis12"
,"target_planet_name":"P23808"},{"fleet_uid":859803,"purpose":0,"ower":1,"time":508,"con_time":647,"from"
:[6,300,2],"target":[6,300,6],"start_user_id":20457507089,"target_user_id":20510481089,"start_planet_name"
:"Tweenis12","target_planet_name":"P23808"}],"count":2}
if (data.fleet) {
var targets = data.fleet.map(function (item) {
return item.target.join('_');
});
}
// for the purpose of demonstration
document.getElementById('targets').innerHTML = targets;
var targets = [<div id="targets"></div>];
source to share
If the data is a link to the answer, then
if(data.target && data.target.length){
data.target = target.join('_');
}
If you want X to replace characters,
var target = data.target;
if(target && target.length){
target = target.join('_');
target = target.replace(/\d/g, 'x')
}
data.target = target;
source to share
Render your json here - http://jsonmate.com/permalink/553846afaa522bae3683edc8
This is how I first figured out the dot notation for accessing json objects in Javascript. In my code below, I follow the path of this visualization to your targets. Fleets are arrays. Arrays are also targets.
var data = {"error":0,"fleet":[{"fleet_uid":859805,"purpose":0,"ower":1,"time":520,"con_time":647,"from":[6,300
,2],"target":[6,300,6],"start_user_id":20457507089,"target_user_id":20510481089,"start_planet_name":"Tweenis12"
,"target_planet_name":"P23808"},{"fleet_uid":859803,"purpose":0,"ower":1,"time":508,"con_time":647,"from"
:[6,300,2],"target":[6,300,6],"start_user_id":20457507089,"target_user_id":20510481089,"start_planet_name"
:"Tweenis12","target_planet_name":"P23808"}],"count":2};
var targets = []; //array of targets in fleets
for (var i = 0; i < data.fleet.length; i++) {
var a = data.fleet[i].target[0];
var b = data.fleet[i].target[1];
var c = data.fleet[i].target[2];
var target = a + "_" + b + "_" + c;
targets.push(target);
}
alert(targets);
source to share