How to update Nth Element of an array

Priya Jha 896 Reputation points
2023-02-22T14:15:50.9766667+00:00

HI,

Is it possible to update/replace the nth element of an array

 

input =[1,2,3,4]

 

Update 3rd element from 3 to 5

 

Output=[1,2,5,4]]

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,373 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Nandan Hegde 36,146 Reputation points MVP Volunteer Moderator
    2023-02-22T14:49:53.23+00:00

    User's image

    {
        "name": "UpdateArray",
        "properties": {
            "activities": [
                {
                    "name": "Set variable1",
                    "type": "SetVariable",
                    "dependsOn": [],
                    "userProperties": [],
                    "typeProperties": {
                        "variableName": "Inter",
                        "value": {
                            "value": "@take(pipeline().parameters.IA,int(pipeline().parameters.ElementNbr))",
                            "type": "Expression"
                        }
                    }
                },
                {
                    "name": "ForEach1",
                    "type": "ForEach",
                    "dependsOn": [
                        {
                            "activity": "Set variable1",
                            "dependencyConditions": [
                                "Succeeded"
                            ]
                        }
                    ],
                    "userProperties": [],
                    "typeProperties": {
                        "items": {
                            "value": "@variables('Inter')",
                            "type": "Expression"
                        },
                        "isSequential": true,
                        "activities": [
                            {
                                "name": "Append variable1",
                                "type": "AppendVariable",
                                "dependsOn": [],
                                "userProperties": [],
                                "typeProperties": {
                                    "variableName": "Final",
                                    "value": {
                                        "value": "@item()",
                                        "type": "Expression"
                                    }
                                }
                            }
                        ]
                    }
                },
                {
                    "name": "Append variable2",
                    "type": "AppendVariable",
                    "dependsOn": [
                        {
                            "activity": "ForEach1",
                            "dependencyConditions": [
                                "Succeeded"
                            ]
                        }
                    ],
                    "userProperties": [],
                    "typeProperties": {
                        "variableName": "Final",
                        "value": {
                            "value": "@pipeline().parameters.IS",
                            "type": "Expression"
                        }
                    }
                },
                {
                    "name": "Set variable2",
                    "type": "SetVariable",
                    "dependsOn": [
                        {
                            "activity": "Append variable2",
                            "dependencyConditions": [
                                "Succeeded"
                            ]
                        }
                    ],
                    "userProperties": [],
                    "typeProperties": {
                        "variableName": "Inter",
                        "value": {
                            "value": "@skip(pipeline().parameters.IA,add(int(pipeline().parameters.ElementNbr),1))",
                            "type": "Expression"
                        }
                    }
                },
                {
                    "name": "ForEach2",
                    "type": "ForEach",
                    "dependsOn": [
                        {
                            "activity": "Set variable2",
                            "dependencyConditions": [
                                "Succeeded"
                            ]
                        }
                    ],
                    "userProperties": [],
                    "typeProperties": {
                        "items": {
                            "value": "@variables('Inter')",
                            "type": "Expression"
                        },
                        "isSequential": true,
                        "activities": [
                            {
                                "name": "Append variable1_copy1",
                                "type": "AppendVariable",
                                "dependsOn": [],
                                "userProperties": [],
                                "typeProperties": {
                                    "variableName": "Final",
                                    "value": {
                                        "value": "@item()",
                                        "type": "Expression"
                                    }
                                }
                            }
                        ]
                    }
                }
            ],
            "parameters": {
                "IA": {
                    "type": "array",
                    "defaultValue": [
                        1,
                        2,
                        3,
                        4,
                        5,
                        9
                    ]
                },
                "IS": {
                    "type": "string",
                    "defaultValue": "6"
                },
                "ElementNbr": {
                    "type": "string",
                    "defaultValue": "3"
                }
            },
            "variables": {
                "Inter": {
                    "type": "Array"
                },
                "Final": {
                    "type": "Array"
                }
            },
            "annotations": []
        }
    }
    
    

  2. KranthiPakala-MSFT 46,642 Reputation points Microsoft Employee Moderator
    2023-02-24T22:51:23.2333333+00:00

    Hi @Priya Jha ,

    Thanks for using Microsoft Q&A forum and posting your query.

    I would go with Nandan Hegdes solution if you are referring to regular pipeline expressions. In case if you are using Mapping data flows and would like to achieve your requirement then you can use MapIndex function in Expression builder.

    Here is a sample: User's image

    User's image

    Hope this helps.


    Please don’t forget to Accept Answer and Yes for "was this answer helpful" wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.