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

编程入门 行业动态 更新时间:2024-10-10 07:21:57

将对象复制到javascript中的另一个对<a href=https://www.elefans.com/category/jswz/34/1770842.html style=象中"/>

将对象复制到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中的另一个对象中

本文发布于:2024-05-07 12:25:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:象中   对象   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!