bafix2203
bafix2203

Reputation: 539

Ascribe string to variables Angular Javascript

Here's my json :

[
    {
        "name": "1QQQJohnQQQ11_22_1998",
        "age" : "ads"
    },
    {
        "name": "2QQQEvaQQQ05_01_1989",
        "age" : "ads"
    },
    {
        "name": "3QQQCasperQQQ02_16_1994",
        "age" : "ads"
    },
    {
        "name": "4QQQBeanQQQ30_12_1996",
        "age" : "ads"
    }]

I do from that a table, and what i have to do is split "QQQ" from name and change it on 3 strings (ex. "4", "Bean", "30_12_1996"), How do it in js file, by ascribe each string to variable (ex. type = "4", name = "Bean", date = "30_12_1996). My js file:

var app = angular.module('app', []);
    app.service('service', function($http, $q){
        var deferred = $q.defer();

        $http.get("filejson.json").then(function(data){
            deferred.resolve(data);
        });

        this.getNames = function(){
            return deferred.promise;
        }
    });
    app.controller('MainCtrl', function($scope, service){
        var promise = service.getNames();
        promise.then(function(data){
            $scope.names = data.data;
            console.log($scope.names);
        });
  //*what i try to do (it didn't works):
 var type = names.name.split('QQQ')[0];
 var name= names.name.split('QQQ')[1];
 var date= names.name.split('QQQ')[2];
 //*
});

Upvotes: 0

Views: 33

Answers (2)

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27202

Use JavaScript **map() method :**

working demo :

var a = [
    {
        name: "1QQQJohnQQQ11_22_1998",
        age : "ads"
    },
    {
        name: "2QQQEvaQQQ05_01_1989",
        age : "ads"
    },
    {
        name: "3QQQCasperQQQ02_16_1994",
        age : "ads"
    },
    {
        name: "4QQQBeanQQQ30_12_1996",
        age : "ads"
    }];
    
a.map(function(item) {
  item.type = item.name.split('QQQ')[0];
  item.date = item.name.split('QQQ')[2];
  item.name = item.name.split('QQQ')[1];
});    
console.log(a);    

Upvotes: 0

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You can make use of the javascript map function to map the array from one format to another and use the string split method.

var a = [
    {
        name: "1QQQJohnQQQ11_22_1998",
        age : "ads"
    },
    {
        name: "2QQQEvaQQQ05_01_1989",
        age : "ads"
    },
    {
        name: "3QQQCasperQQQ02_16_1994",
        age : "ads"
    },
    {
        name: "4QQQBeanQQQ30_12_1996",
        age : "ads"
    }];


var result = a.map(function (value, index, array) { 
                    varsplitValue = value.name.split("QQQ");
                    value["SNo"] = varsplitValue[0];
                    value["personName"] = varsplitValue[1]; 
                    value["dob"] = varsplitValue[2];
                    return value;
} );

result contains the array after calling the map function.

result[0].SNO would display the SNO of first person (1).

result[0].personName would display the first person name (John).

result[0].dob would display the first person date of birth (11_22_1998).

Upvotes: 1

Related Questions