m = [{"text":"mamamia","speaker":"","id":0},{"text":"mamamia","speaker":"","id":1},{"text":"mamamia","speaker":"","id":2},{"text":"mamamia","speaker":"","id":3},{"text":"mamamia","speaker":"","id":4},{"text":"mamamia","speaker":"","id":5}]
我想打印“ id”的所有值。从0 -5开始。然后将其更改为6-11。然后将新值附加到旧值并将其保存。有可能做到吗?
答案 0 :(得分:0)
m.map(function(obj) {
console.log(obj.id);
obj.id = obj.id + 6;
})
答案 1 :(得分:0)
因为它是 json字符串,所以我更喜欢将其用作反序列化对象,如下所示:
string = '[{"text":"mamamia","speaker":"","id":0},{"text":"mamamia","speaker":"","id":1},{"text":"mamamia","speaker":"","id":2},{"text":"mamamia","speaker":"","id":3},{"text":"mamamia","speaker":"","id":4},{"text":"mamamia","speaker":"","id":5}]'
deserialized = json.loads(string)
for obj in deserialized:
obj["id"] = obj.get("id") + 6
print(json.dumps(deserialized))
输出:
[{"id": 6, "text": "mamamia", "speaker": ""}, {"id": 7, "text": "mamamia", "speaker": ""}, {"id": 8, "text": "mamamia", "speaker": ""}, {"id": 9, "text": "mamamia", "speaker": ""}, {"id": 10, "text": "mamamia", "speaker": ""}, {"id": 11, "text": "mamamia", "speaker": ""}]
答案 2 :(得分:-1)
yes it is passible to php see below example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$m=[ ["text"=>"mamamia","speaker"=>"a","id"=>0],
["text"=>"mamamia","speaker"=>"b","id"=>1],
["text"=>"mamamia","speaker"=>"c","id"=>2],
["text"=>"mamamia","speaker"=>"d","id"=>3],
["text"=>"mamamia","speaker"=>"e","id"=>4],
["text"=>"mamamia","speaker"=>"f","id"=>5]
];
//echo gettype($m)."<br>";
$myJSON = json_encode($m);
//echo $myJSON;
$myJSON=json_decode($myJSON);
echo "<table border='2px'><tr><th>Text</th><th>Speeker</th><th>ID</th></tr>";
foreach($myJSON as $a)
{
echo"<tr><td>". $a->text."</td><td>".$a->speaker."</td><td>".$a->id."</td>
</tr>";
}
echo "</table>";
//add the id 6 - 11
array_push($m,["text"=>"mamamia","speaker"=>"a","id"=>6]);
array_push($m,["text"=>"mamamia","speaker"=>"a","id"=>7]);
array_push($m,["text"=>"mamamia","speaker"=>"a","id"=>8]);
array_push($m,["text"=>"mamamia","speaker"=>"a","id"=>9]);
array_push($m,["text"=>"mamamia","speaker"=>"a","id"=>10]);
array_push($m,["text"=>"mamamia","speaker"=>"a","id"=>11]);
//print_r($m);
$myJSON = json_encode($m);
//echo $myJSON."<br>";
$myJSON=json_decode($myJSON);
echo "<table border='2px'><tr><th>Text</th><th>Speeker</th><th>ID</th>
</tr>";
foreach($myJSON as $a)
{
//print_r($a);
echo"<tr><td>". $a->text."</td><td>".$a->speaker."</td><td>".$a->id."
</td></tr>";
}
echo "</table>";
?>
</body>
</html>
**output**
[Output we get]
[1]: https://i.stack.imgur.com/CU9k7.jpg