javascript - 如何操作json中的某个元素
大家讲道理
大家讲道理 2017-04-11 09:43:36
0
6
384

有这样一个json:


    [
            {
                groupname:'主要任务',
                index:0,
                tasks:[
                    {
                        id:2,
                        title:'完善通讯录aaweeeeg',
                        urgency:'正常',
                        remark:'some word here',
                        tasker_main:'henry',
                        tasker_other:['jim','john'],
                        participators:['1','2','3'],
                        publisher:'henry',
                        ddl:'1288323623006',
                        base_score:'5',
                        upvoters:['jim','小华'],
                        completed:true
                    },
                    {
                        id:3,
                        title:'task2',
                        urgency:'正常',
                        remark:'',
                        tasker_main:'12',
                        tasker_other:['小李','小王'],
                        participators:['1','2','3'],
                        publisher:'小华',
                        ddl:'1289355623006',
                        base_score:3,
                        upvoters:[],
                        completed:true
                    },
                    {
                        id:4,
                        title:'紧急会议',
                        urgency:'紧急',
                        remark:'some word here',
                        tasker_main:'henry',
                        tasker_other:['smith','harry'],
                        participators:['1','2','3'],
                        publisher:'小明',
                        ddl:'1285978923006',
                        base_score:2,
                        upvoters:['henry'],
                        completed:false
                    }
                ]
            },
            {
                groupname:'技术任务',
                index:1,
                tasks:[
                    {
                        id:5,
                        title:'task1',
                        urgency:'火急',
                        remark:'some word here',
                        tasker_main:'henry',
                        tasker_other:['jim','john'],
                        participators:['1','2','3'],
                        publisher:'mary',
                        ddl:'1285978923006',
                        base_score:'5',
                        upvoters:['jim','小华'],
                        completed:false
                    }
                ]
            }
        ]

我想通过task的id这个属性,找到特定的task对象,然后把它的completed属性改成true。
请问该如何实现这个功能啊?

PS.这个json实际上是当对象来用的,不是字符串。。我是想用JavaScript,在用户单击按钮时,把相应的task标记成已完成。

谢谢

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(6)
巴扎黑
   var  data =  [
            {
                groupname:'主要任务 
                index:0,
                tasks:[
                    {
                        id:2,
                        title:'完善通讯录aaweeeeg',
                        urgency:'正常',
                        remark:'some word here',
                        tasker_main:'henry',
                        tasker_other:['jim','john'],
                        participators:['1','2','3'],
                        publisher:'henry',
                        ddl:'1288323623006',
                        base_score:'5',
                        upvoters:['jim','小华'],
                        completed:true
                    },
                    {
                        id:3,
                        title:'task2',
                        urgency:'正常',
                        remark:'',
                        tasker_main:'12',
                        tasker_other:['小李','小王'],
                        participators:['1','2','3'],
                        publisher:'小华',
                        ddl:'1289355623006',
                        base_score:3,
                        upvoters:[],
                        completed:true
                    },
                    {
                        id:4,
                        title:'紧急会议',
                        urgency:'紧急',
                        remark:'some word here',
                        tasker_main:'henry',
                        tasker_other:['smith','harry'],
                        participators:['1','2','3'],
                        publisher:'小明',
                        ddl:'1285978923006',
                        base_score:2,
                        upvoters:['henry'],
                        completed:false
                    }
                ]
            },
            {
                groupname:'技术任务',
                index:1,
                tasks 
                    {
                        id:5,
                        title:'task1',
                        urgency:'火急',
                        remark:'some word here',
                        tasker_main:'henry',
                        tasker_other:['jim','john'],
                        participators:['1','2','3'],
                        publisher:'mary',
                        ddl:'1285978923006',
                        base_score:'5',
                        upvoters:['jim','小华'],
                        completed:false
                    }
                ]
            }
        ]
   var _id = 4; //把id是4的变成true
   for(var i=0;i<data.length;i++){
       for(var j=0;j<data[i].tasks.length;j++){
           if(data[i].tasks[j]['id'] == _id){
               data[i].tasks[j]['completed'] = true;
           }
           
       }
       
   }
Peter_Zhu
var data = [{
  groupname: '主要任务',
  index: 0,
  tasks: [{
    id: 2,
    title: '完善通讯录aaweeeeg',
    urgency: '正常',
    remark: 'some word here',
    tasker_main: 'henry',
    tasker_other: ['jim', 'john'],
    participators: ['1', '2', '3'],
    publisher: 'henry',
    ddl: '1288323623006',
    base_score: '5',
    upvoters: ['jim', '小华'],
    completed: true
  }, {
    id: 3,
    title: 'task2',
    urgency: '正常',
    remark: '',
    tasker_main: '12',
    tasker_other: ['小李', '小王'],
    participators: ['1', '2', '3'],
    publisher: '小华',
    ddl: '1289355623006',
    base_score: 3,
    upvoters: [],
    completed: true
  }, {
    id: 4,
    title: '紧急会议',
    urgency: '紧急',
    remark: 'some word here',
    tasker_main: 'henry',
    tasker_other: ['smith', 'harry'],
    participators: ['1', '2', '3'],
    publisher: '小明',
    ddl: '1285978923006',
    base_score: 2,
    upvoters: ['henry'],
    completed: false
  }]
}, {
  groupname: '技术任务',
  index: 1,
  tasks: [{
    id: 5,
    title: 'task1',
    urgency: '火急',
    remark: 'some word here',
    tasker_main: 'henry',
    tasker_other: ['jim', 'john'],
    participators: ['1', '2', '3'],
    publisher: 'mary',
    ddl: '1285978923006',
    base_score: '5',
    upvoters: ['jim', '小华'],
    completed: false
  }]
}];

function findById(id){
  return id && map[id];
}
// data to map index is `id`
function dataToMap(data){
  data.forEach(function(group, index){
    group.tasks.forEach(function(task){
      var id = task.id;
      map[id] = task;
    })
  })
}
var map = {};

dataToMap(data);
console.log(findById(3));
刘奇

a[1]['tasks'][0]['id']

浏览器 F12 跑一边结构不就出来了 ?

Ty80
  1. 解析成 JS 对象

  2. 遍历查找指定 id 对象

  3. 修改属性

  4. 转换回 JSON 对象

PHPzhong

a[0].tasks[0].completed

阿神

json能是个数组吗?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!