-1

I am passing data from php to js via ajax. I use json_encode before the transfer. here the example:

$data = [
    [
        "path" => "/TestMenu1",
        "component" => 'test1',
        "children" => [[
            "path" => "/api/sub/route",
            "component" => "test2",
        ]]
    ],[
        "path" => "/api/findme/2",
        "component" => "test3",
    ]
]; 

And from client side I get this without any problems:

[
  {
    "path": "/TestMenu1",
    "component": "test1",
    "children": [
      {
        "path": "/api/sub/route",
        "component": "test2"
      }
    ]
  },
  {
    "path": "/api/findme/2",
    "component": "test3"
  }
]

But I need the components to be variable names instead string like:

[
  {
    "path": "/TestMenu1",
    "component": test1,
    "children": [
      {
        "path": "/api/sub/route",
        "component": test2
      }
    ]
  },
  {
    "path": "/api/findme/2",
    "component": test3
  }
]

Is that possible? If yes how can I accomplish that?

4
  • JSON.encode is not a function.in either PHP or JavaScript. Commented Sep 21, 2017 at 12:39
  • 1
    Possible duplicate of How to get jSON response into variable from a jquery script Commented Sep 21, 2017 at 12:42
  • Yep you are right @Script47 its actually json_encode(). Commented Sep 21, 2017 at 12:43
  • What you are asking is not currently possible. You would have to write your own version of JSON encode and somehow mark which values are variables for use by JS. This would defeat the purpose and standardized use of JSON so you cannot call it JSON anymore. Commented Sep 21, 2017 at 12:44

3 Answers 3

1

You will need to make intermediate transformation of response you get from server to extended data structure with component name replaced with real component instance. For this make sure components are available in helper object map and individual component objects are accessible by its name.

Here is simple example of this approach:

// Response you get from server
const response = [
  {
    "path": "/TestMenu1",
    "component": "test1",
    "children": [
      {
        "path": "/api/sub/route",
        "component": "test2"
      }
    ]
  },
  {
    "path": "/api/findme/2",
    "component": "test3"
  }
]

// Map of component name to component instance
const components = {
  test1: { name: 'test1' },
  test2: { name: 'test2' },
  test3: { name: 'test3' }
}

// Recursive function to resolve component by its name from the map
const mapComponents = (items, componentMap) => {
  return items.map(item => {
    item.component = componentMap[item.component]
    
    if (item.children && item.children.length) {
      item.children = mapComponents(item.children, componentMap)
    }
    
    return item
  })
}

// Final result
const result = mapComponents(response, components)

console.log(result)
.as-console-wrapper {min-height: 100%}

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

5 Comments

@dsfq In my case the component: value (has to) represents an imported object. The component has no use of name prop. Did i misunderstand you?
It was just an example, just some object (with prop name), it can be whatever you need. Import your objects, maybe even export them in the first place as a map, done.
There are a lot of imports. I import them with: import test1 from '../../components/test1'; . how can I export them in the first place as a map
Woww! now that worked for me. First I imported tests as usual with import test1 from "test1"at the beginning. Then I created components object as you said and put the imports in it. const components = { test1, test2, test3 }. And then I used the mapComponents loop you gave. Voila! it works. Huge thanks @dfsq . I really appreciate that.
Cool, glad you figured it out :)
1

You can achieve it by window[obj.component]. like this

var test1 = 111;
var test2 = 222;
var test3 = 333;
var response = [
  {
    "path": "/TestMenu1",
    "component": "test1",
    "children": [
      {
        "path": "/api/sub/route",
        "component": "test2"
      }
    ]
  },
  {
    "path": "/api/findme/2",
    "component": "test3"
  }
];
var newObj = [];
function getCompenentVal(obj){
  obj.component = window[obj.component];
  if(obj.children !== undefined){
    var temp = [];
    for(var i in obj.children){
      temp.push(getCompenentVal(obj.children[i]));
    }
    obj.children = temp;
  }
  return obj;
}
for(var i in response){
  newObj.push(getCompenentVal(response[i]));
}
console.log(newObj);

4 Comments

actually this is prerendered by webpack node instead browser. thus that wont help.
@Skeletor : what you are saying, is not clear. please be specific that where you want to replace variable in php or javascript, I think now you want to replace string as variable in php ?
It is client side. I want to replace the variable in the javascript side. I am using this for a vue component which requires the component value to be a object (generally imported one). I tried the window[] but it does not work for me, it returns undefined.
Just make one live demo with vuejs
0

you can assign new value to the field you want

Php code

$data = [
    [
        "path" => "/TestMenu1",
        "component" => 'test1',
        "children" => [[
            "path" => "/api/sub/route",
            "component" => "test2",
        ]]
    ],[
        "path" => "/api/findme/2",
        "component" => "test3",
    ]
]; 


echo json_encode($data);



?>

output from php code

[{"path":"\/TestMenu1","component":"test1","children":[{"path":"\/api\/sub\/route","component":"test2"}]},{"path":"\/api\/findme\/2","component":"test3"}]

and with the output in javascript

var data = '[{"path":"\/TestMenu1","component":"test1","children":[{"path":"\/api\/sub\/route","component":"test2"}]},{"path":"\/api\/findme\/2","component":"test3"}]';

var newdata = JSON.parse(data);
newdata.forEach(function(values) {
    values.component = 'new value'

     if(values.children!=undefined) {
         values.children.forEach(function(innervalues) {
              innervalues.component = 'new value'
         })
    }
});

console.log(newdata)

output in console like

   [
  {
    "path": "/TestMenu1",
    "component": "new value",
    "children": [
      {
        "path": "/api/sub/route",
        "component": "new value"
      }
    ]
  },
  {
    "path": "/api/findme/2",
    "component": "new value"
  }
]

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.