0

I’m trying to create a data object in javascript that matches the construction from the php script below and need help with adding the ‘depends’ elements under object ‘name’.

var data = [{}];
var json = [
{ "type": "type1", "group": "group1", "name": "name1", "depends": ["x","y","z"] },
{ "type": "type2", "group": "group2", "name": "name2", "depends": ["a","b","c"] },
{ "type": "type3", "group": "group3", "name": "name3", "depends": ["q"] }]

json.forEach(function (item) {
    data.push(item.name);
});


json.forEach(function (item) {
    item.forEach(function (item.depends) {
        data.push(item.depends);
    });
});

PHP

foreach ($json as $obj) {
    $data[$obj['name']] = $obj;
}

foreach ($data as &$obj) {
    $obj['dependedOnBy'] = array();
}

unset($obj);
foreach ($data as &$obj) {
    foreach ($obj['depends'] as $name) {
        if ($data[$name]) {
            $data[$name]['dependedOnBy'][] = $obj['name'];
        } else {
            $errors[] = "Unrecognized dependency: '$obj[name]' depends on '$name'";
        }
    }
 }
2
  • 1
    function (item.depends) {?. That's not even valid syntax... Commented Aug 15, 2014 at 7:11
  • what is the expected output? Commented Aug 15, 2014 at 9:17

1 Answer 1

1

From my understanding, this is what your php code does:

var data = {};
json.forEach(function (item) {
    item.dependedOnBy = [];
    data[item.name] = item;
});

var errors = [];
Object.keys(data).forEach(function (name) {
    data[name].depends.forEach(function (dep) {
        if(!data[dep])
            errors.push( "Unrecognized dependency: " + name + " depends on " + dep);
        else
            data[name].dependedOnBy.push(data[dep]);
    });
});

http://jsfiddle.net/yqqdr5bt/

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

6 Comments

So I'm not the only one using Object.keys(data).forEach(), nice!
Fantastic. I'm still on a steep learning curve! Any ideas why the output doesn't quite match the structure from the php code with json below? javascript object {name1: Object, name2: Object, x: Object, y:Object, a: Object...} php Object {data: Object, errors: Array[0]}
@user3359706: the output doesn't match your example, could you edit the post and add some real data? Also, try console.log(JSON.stringify(data)) instead of just console.log.
Sorry I should have included this: var json = [ { "type": "type1", "group": "group1", "name": "name1", "depends": ["x","y"] }, { "type": "type2", "group": "group2", "name": "name2", "depends": ["a","b"] }, { "type": "type3", "group": "group3", "name": "x", "depends": [] }, { "type": "type3", "group": "group3", "name": "y", "depends": [] }, { "type": "type3", "group": "group3", "name": "a", "depends": [] }, { "type": "type3", "group": "group3", "name": "b", "depends": [] } ] And I missed this. echo json_encode(array( 'data' => $data, 'errors' => $errors
@user3359706: see the fiddle.
|

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.