最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

将对象复制到javascript中的另一个对象中

IT培训 admin 11浏览 0评论

将对象复制到javascript中的另一个对象中

我是javascript的新手。我尽力了,仍然没有得到正确的输出。我将结果作为对象。它有2个值。第一个是数组items中元素的计数。我想将以P开头的项目复制到另一个对象中

var beginEnd="P";
var result = {
    count: 5,
    items: [
        {
            "organizationCode": "FP1",
            "organizationName": "FTE Process Org"
        },
        {
            "organizationCode": "T11",
            "organizationName": "FTE Discrete Org"
        },
        {
            "organizationCode": "PD2",
            "organizationName": "Product development Org"
        },
        {
            "organizationCode": "PD1",
            "organizationName": "Product1 development Org"
        },
        {
            "organizationCode": "MD1",
            "organizationName": "Main development Org"
        }
    ]
};
console.log(result);
var arr = [];
for (var i = 0; i < result.length; i++) {
    if (result[i].startWith(beginEnd)) {
        arr[i++] = result[i];
    }
}
console.log(arr);

我尝试了上面的代码,但给出的是未定义的。

我想要下面的输出。

{
    count: 2,
    items:
     [
        {
            "organizationCode": "PD2",
            "organizationName": "Product development Org"
        },
        {
            "organizationCode": "PD1",
            "organizationName": "Product1 development Org"
        }
    ]
}
回答如下:

据我所知,在这段代码中,您没有按预期方式访问对象属性

 if (result[i].startWith(beginEnd)) {

您正在直接访问结果对象,但是您真正需要做的是访问result.items [i],记住要看一下嵌套字段。

将对象复制到javascript中的另一个对象中

我是javascript的新手。我尽力了,仍然没有得到正确的输出。我将结果作为对象。它有2个值。第一个是数组items中元素的计数。我想将以P开头的项目复制到另一个对象中

var beginEnd="P";
var result = {
    count: 5,
    items: [
        {
            "organizationCode": "FP1",
            "organizationName": "FTE Process Org"
        },
        {
            "organizationCode": "T11",
            "organizationName": "FTE Discrete Org"
        },
        {
            "organizationCode": "PD2",
            "organizationName": "Product development Org"
        },
        {
            "organizationCode": "PD1",
            "organizationName": "Product1 development Org"
        },
        {
            "organizationCode": "MD1",
            "organizationName": "Main development Org"
        }
    ]
};
console.log(result);
var arr = [];
for (var i = 0; i < result.length; i++) {
    if (result[i].startWith(beginEnd)) {
        arr[i++] = result[i];
    }
}
console.log(arr);

我尝试了上面的代码,但给出的是未定义的。

我想要下面的输出。

{
    count: 2,
    items:
     [
        {
            "organizationCode": "PD2",
            "organizationName": "Product development Org"
        },
        {
            "organizationCode": "PD1",
            "organizationName": "Product1 development Org"
        }
    ]
}
回答如下:

据我所知,在这段代码中,您没有按预期方式访问对象属性

 if (result[i].startWith(beginEnd)) {

您正在直接访问结果对象,但是您真正需要做的是访问result.items [i],记住要看一下嵌套字段。

发布评论

评论列表 (0)

  1. 暂无评论