1
[
    {
        "uId": "2",
        "tabId": 1,
        "tabName": "Main",
        "points": "10"
    },
    {
        "uId": "3",
        "tabId": 2,
        "tabName": "Photography",
        "points": "20"
    }
]

how can I insert into specified array by inspecting its properties values? says I want to add a assoc object into uId = 3, how can I do that? or it's not possible technically?

2

4 Answers 4

2

This is also possible using array.map (Added to the ECMA-262 standard in the 5th edition):

array.map(function(i){
    if(i.uId == 3) i['newprop'] = 'newValue';
});

Example Here.

Update: It could be an array

if(i.uId == 3) i['newprop'] = ['newvalue1', 'newvalue2'];

Example2 Here.

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

2 Comments

can 'newValue' be an array?
Yes, it could be an array @user3522444.
1

They look like JSON data , so json_decode() to an array , search for the UId value and then add the corresponding assoc value and after the end finally wrap them up using json_encode()

foreach($array as $k=>&$arr)
{
    if($arr->{'uId'}==2)
    {
        $arr->{'somecol'}="Hey";
    }
}
echo json_encode($array,JSON_PRETTY_PRINT);

OUTPUT :

[
    {
        "uId": "2",
        "tabId": 1,
        "tabName": "Main",
        "points": "10",
        "somecol": "Hey"
    },
    {
        "uId": "3",
        "tabId": 2,
        "tabName": "Photography",
        "points": "20"
    }
]

4 Comments

$array =json_decode($json,true); my $json is already an array, but when I use that it says json_decode() expects parameter 1 to be string, array given i
Then don't do $array =json_decode($json,true); , just run the code from the foreach
Cannot use object of type stdClass as array
@user3522444, Please see the edited answer. (Tested)
1
var array = [
    {
        "uId": "2",
        "tabId": 1,
        "tabName": "Main",
        "points": "10"
    },
    {
        "uId": "3",
        "tabId": 2,
        "tabName": "Photography",
        "points": "20"
    }
];

for ( var i = 0; i < array.length; i++ ) {
    if ( array[i].uId == 3) {
        array[i].someProp = "Hello";
        break; // remove this line for multiple updates
    }
}

Or you can make a function like this:

function getMatch(data, uid) {
    for ( var i = 0; i < data.length; i++ ) {
        if ( data[i].uId == 3) {
            return data[i];
        }
    }
}

and use it like this:

getMatch(array, 3).someproperty = 4;

1 Comment

You can also add console.log(JSON.stringify(array)); after the for loop to see your updated array
1

You can use the map function, which executes a function on each element of an array

a.map(function(el) { 
  if (el.uId == 3) {
    el.prop = "value";
  }
});

Or you can use the filter function.

// Get the array of object which match the condition
var matches = a.filter(function(x) { return x.uId == 3 });
if (matches.length > 0) {
    matches[0].prop = "value";
}

1 Comment

Sorry but you are a moderator but I didn't notice you in the election but I could be wrong, anyways, welcome :-)

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.