0

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];
 //*
});
2
  • Splitting the string like that should work. I think your problem is elsewhere. When you say it doesn't work what do you mean? What is the error? What is the output? Commented Dec 22, 2016 at 21:48
  • did you checked my answer ? Commented Jan 6, 2017 at 3:42

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

Comments

0

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);    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.